2017-08-25 130 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)