2017-08-25 127 views
-1

原諒我的代碼,但我只是讓我的朋友自動填充一個GUI界面,歌曲信息,每首歌曲的頻道信息,以及諸如附在歌曲上的圖像。現在我只是從Youtube上的播放列表和Soundcloud上的播放列表中刪減。我所有這些工作都正常工作,但是我對前端開發不熟悉,讓我處於一個糟糕的境地,爲他提供了一個體面的應用程序。我有很多想法,我可以完成,但現在我只是創建按鈕,每個歌曲標題作爲文本。 Here is an image of my progress.我仍然需要找到一種方法將每個圖像附加到on_enter事件的每個按鈕上,但這是爲了以後。正如你所看到的,我有一個on_leave函數註釋掉了。我每次離開按鈕時都使用它來刪除self.image_window。問題是甚至鼠標移動的極小數量都會導致窗口被刪除並重新創建數十次。我如何將它設置爲靜態,所以當我將鼠標懸停在按鈕上時,它不會創建/刪除窗口。Tkinter <enter>和<leave>事件無法正常工作

謝謝!提供JSON文件的

from Tkinter import * 
import json 
import os 
import webbrowser 


class GUIPopulator(Frame): 
    def __init__(self, parent): 
     Frame.__init__(self) 
     self.parent = parent 
     self.configure(bg='PeachPuff2') 
     self.columnconfigure(20, weight=1) 
     self.rowconfigure(30, weight=1) 
     self.curtab = None 
     self.tabs = {} 
     self.pack(fill=BOTH, expand=1, padx=5, pady=5) 

     self.column = 0 
     self.row = 0 

    def on_enter(self, event): 

     self.image_window = Toplevel(self) 
     self.img_path = os.getcwd() + '/Rotating_earth_(large).gif' 
     self.img = PhotoImage(file=self.img_path) 

     #self.image_window.minsize(width=200, height=250) 
     self.image_window.title("Preview") 
     canvas = Canvas(self.image_window, width=200, height=200) 
     canvas.pack() 
     canvas.create_image(100, 100, image=self.img) 
    #def on_leave(self, enter): 



    def addTab(self, id): 
     tabslen = len(self.tabs) 

     tab = {} 
     if self.row < 30: 
      btn = Button(self, text=id,highlightbackground='PeachPuff2' ,command=lambda: self.raiseTab(id)) 

      btn.grid(row=self.row, column=self.column, sticky=W+E) 

      btn.bind("<Enter>", self.on_enter) 

      #btn.bind("<Leave>", self.on_leave) 


      tab['id']=id 
      tab['btn']=btn 



      self.tabs[tabslen] = tab 
      self.raiseTab(id) 
      self.row +=1 
     else: 
      self.row = 0 
      self.column +=1 
      btn = Button(self, text=id,highlightbackground='PeachPuff2' ,command=lambda: self.raiseTab(id)) 

      btn.grid(row=self.row, column=self.column, sticky=W+E) 

      tab['id']=id 
      tab['btn']=btn 

      self.tabs[tabslen] = tab 
      self.raiseTab(id) 


    def raiseTab(self, tabid): 
     with open(os.getcwd() + '/../PlaylistListener/CurrentSongs.json') as current_songs: 
      c_songs = json.load(current_songs) 

     print(tabid) 
     if self.curtab!= None and self.curtab != tabid and len(self.tabs)>1: 
      try: 
       #webbrowser.open(c_songs[tabid]['link']) 
       webbrowser.open_new('http://youtube.com') 
      except: 
       pass 



def main(): 
    root = Tk() 
    root.title('Playlist Scraper') 
    root.geometry("1920x1080+300+300") 
    t = GUIPopulator(root) 

    with open(os.getcwd() + '/../PlaylistListener/CurrentSongs.json') as current_songs: 
     c_songs = json.load(current_songs) 

    for song in c_songs: 
      t.addTab(song) 

    root.mainloop() 

if __name__ == '__main__': 
    main() 

例子:

{ 
    "F\u00d8RD - Shadows (feat. Samsaruh)": { 
    "page_title": "youtube", 
    "link": "youtube.com/watch?v=CNiV6Pne50U&index=32&list=PLkx04k4VGz1tH_pnRl_5xBU1BLE3PYuzd", 
    "id": "CNiV6Pne50U", 
    "channel": "youtube.com/watch?v=CNiV6Pne50U&index=32&list=PLkx04k4VGz1tH_pnRl_5xBU1BLE3PYuzd", 
    "image_path": [ 
     "http://i4.ytimg.com/vi/CNiV6Pne50U/hqdefault.jpg", 
     "CNiV6Pne50U.jpg" 
    ] 
    }, 
    "Katelyn Tarver - You Don't Know (tof\u00fb remix)": { 
    "page_title": "youtube", 
    "link": "youtube.com/watch?v=7pPNv38JzD4&index=43&list=PLkx04k4VGz1tH_pnRl_5xBU1BLE3PYuzd", 
    "id": "7pPNv38JzD4", 
    "channel": "youtube.com/watch?v=7pPNv38JzD4&index=43&list=PLkx04k4VGz1tH_pnRl_5xBU1BLE3PYuzd", 
    "image_path": [ 
     "http://i4.ytimg.com/vi/7pPNv38JzD4/hqdefault.jpg", 
     "7pPNv38JzD4.jpg" 
    ] 
    }, 
    "Illenium - Crawl Outta Love (feat. Annika Wells)": { 
    "page_title": "youtube", 
    "link": "youtube.com/watch?v=GprXUDZrdT4&index=7&list=PLkx04k4VGz1tH_pnRl_5xBU1BLE3PYuzd", 
    "id": "GprXUDZrdT4", 
    "channel": "youtube.com/watch?v=GprXUDZrdT4&index=7&list=PLkx04k4VGz1tH_pnRl_5xBU1BLE3PYuzd", 
    "image_path": [ 
     "http://i4.ytimg.com/vi/GprXUDZrdT4/hqdefault.jpg", 
     "GprXUDZrdT4.jpg" 
    ] 
    } 
} 
+1

您已經發布了大量的代碼被問的問題,似乎是無關緊要。請嘗試將其縮減爲不依賴於外部數據文件的[mcve]。 –

+1

而不是每次你的鼠標進入按鈕字段時創建頂層框架,你應該在'__init__'節創建框架,然後'on_entry'將框架與網格管理器放在一起,然後忘記網格。或者你甚至可以根據需要使用'tkraise()'方法來提高或降低頂層。 –

+0

我無法複製您的問題。只要我的鼠標光標停留在按鈕內,「」和「」事件就不會觸發。他們只在我離開窗戶時纔開火。只是要清楚:你是否說過,如果你移動鼠標_而不離開button_的邊界,你會看到''事件觸發? –

回答

0

一些測試中,我想出了一些代碼,我想你可以使用或者是你在找什麼後。

我改變了一些東西並添加了一些其他的東西。

1我們需要爲頂部窗口創建一個佔位符,我們可以稍後在代碼中使用。因此在的__init__部分添加self.image_window = None。我們很快會討論這部分內容。

接下來,我創建了圖像路徑作爲init上的類屬性,如果需要,可以稍後使用更新進行更改。

接下來我添加了一個bind()init,這將幫助我們將self.image_window放置在正確的位置,即使我們移動根窗口。所以我們在__init__部分添加self.parent.bind("<Configure>", self.move_me)

接下來我們創建了我們剛創建了一個bind的方法move_me。 寫它的方式它只會生效,如果self.image_window不等於None這應該防止任何錯誤,而self.image_window正在使用但是我還沒有創建錯誤處理來處理在用戶關閉頂層窗口後發生了什麼。它並不困難,但我想回答手頭的主要問題。

這裏是move_me方法:

def move_me(self, event): 
    if self.image_window != None: 
     h = self.parent.winfo_height() # gets the height of the window in pixels 
     w = self.parent.winfo_width() # gets the width of the window in pixels 

     # gets the placement of the root window then uses height and width to 
     # calculate where to place the window to keep it at the bottom right. 
     self.image_window.geometry('+{}+{}'.format(self.parent.winfo_x() + w - 250, 
                self.parent.winfo_y() + h - 250)) 

接下來我們需要修改on_enter方法來創建頂層窗口,如果出類屬性self.image_window等於None,如果它不等於None那麼我們可以用if語句的else部分只是更新圖像。

下面是修改on_enter方法:

def on_enter(self, event): 

    if self.image_window == None: 
     self.image_window = Toplevel(self) 
     #this keeps the toplevel window on top of the program 
     self.image_window.attributes("-topmost", True) 

     h = self.parent.winfo_height() 
     w = self.parent.winfo_width() 
     self.image_window.geometry('+{}+{}'.format(self.parent.winfo_x() + w - 250, 
                self.parent.winfo_y() + h - 250)) 
     self.img = PhotoImage(file=self.img_path) 
     self.image_window.title("Preview") 
     self.canvas = Canvas(self.image_window, width=200, height=200) 
     self.canvas.pack() 
     self.canv_image = self.canvas.create_image(100, 100, image=self.img) 

    else: 
     self.img = PhotoImage(file= self.img_path) 
     self.canvas.itemconfig(self.canv_image, image = self.img) 

之所以這麼說,有你的代碼需要但是,要解決其他一些問題這個答案應該指向你在正確的方向。

下面是你需要更換你的一段代碼:

class GUIPopulator(Frame): 
    def __init__(self, parent): 
     Frame.__init__(self) 
     self.parent = parent 
     self.configure(bg='PeachPuff2') 
     self.columnconfigure(20, weight=1) 
     self.rowconfigure(30, weight=1) 
     self.curtab = None 
     self.tabs = {} 
     self.pack(fill=BOTH, expand=1, padx=5, pady=5) 

     self.column = 0 
     self.row = 0 

    def on_enter(self, event): 

     self.image_window = Toplevel(self) 
     self.img_path = os.getcwd() + '/Rotating_earth_(large).gif' 
     self.img = PhotoImage(file=self.img_path) 

     #self.image_window.minsize(width=200, height=250) 
     self.image_window.title("Preview") 
     canvas = Canvas(self.image_window, width=200, height=200) 
     canvas.pack() 
     canvas.create_image(100, 100, image=self.img) 
    #def on_leave(self, enter): 

有了這個:

class GUIPopulator(Frame): 
    def __init__(self, parent): 
     Frame.__init__(self) 
     self.parent = parent 
     self.configure(bg='PeachPuff2') 
     self.columnconfigure(20, weight=1) 
     self.rowconfigure(30, weight=1) 
     self.curtab = None 
     self.image_window = None 
     self.img_path = os.getcwd() + '/Rotating_earth_(large).gif' 
     self.tabs = {} 
     self.pack(fill=BOTH, expand=1, padx=5, pady=5) 
     self.parent.bind("<Configure>", self.move_me) 
     self.column = 0 
     self.row = 0 

    def move_me(self, event): 
     if self.image_window != None: 
      h = self.parent.winfo_height() 
      w = self.parent.winfo_width() 
      self.image_window.geometry('+{}+{}'.format(self.parent.winfo_x() + w - 250, 
                 self.parent.winfo_y() + h - 250)) 

    def on_enter(self, event): 

     if self.image_window == None: 
      self.image_window = Toplevel(self) 
      self.image_window.attributes("-topmost", True) 

      h = self.parent.winfo_height() 
      w = self.parent.winfo_width() 
      self.image_window.geometry('+{}+{}'.format(self.parent.winfo_x() + w - 250, 
                 self.parent.winfo_y() + h - 250)) 
      self.img = PhotoImage(file=self.img_path) 
      self.image_window.title("Preview") 
      self.canvas = Canvas(self.image_window, width=200, height=200) 
      self.canvas.pack() 
      self.canv_image = self.canvas.create_image(100, 100, image=self.img) 

     else: 
      self.img = PhotoImage(file= self.img_path) 
      self.canvas.itemconfig(self.canv_image, image = self.img)