请原谅我的代码,但我只是让我的朋友用歌曲信息、每首歌曲的频道信息和歌曲附加的图像等内容自动填充GUI界面。现在我只从Youtube上的播放列表和Soundcloud上的播放列表中抓取。所有这些我都能正常工作,但我对前端开发还不熟悉,这让我陷入了一个可怕的境地,无法为他提出一份像样的申请。我有很多想法,我可以做,但现在我只是创建按钮,每首歌的标题作为文本。Here is an image of my progress.我仍然需要找到一种方法将每个图像附加到on_enter事件的每个按钮上,但这是以后的事情。正如您所看到的,我有一个onéu leave函数被注释掉了。我用它来删除self.image_窗口每次我留下一个按钮。问题是即使是极小的鼠标移动量也会导致窗口被删除并重新创建几十次。如何使其静态,以便当我将鼠标悬停在一个按钮上时,它不会垃圾邮件创建/删除窗口?在
谢谢!在
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()
提供的JSON文件示例:
^{pr2}$
经过一些测试,我想出了一些代码,我认为你可以使用或是你正在寻找的。在
我改变了一些东西,并增加了一些其他的东西。在
首先,我们需要为顶部窗口创建一个占位符,以便稍后在代码中使用。所以在
GUIPopulatior
的__init__
部分中添加self.image_window = None
。我们将很快讨论这一部分。在接下来,我将映像路径创建为
init
上的类属性,如果需要,可以稍后通过更新进行更改。在接下来,我在}放在正确的位置。所以我们也将
init
中添加了一个bind()
,这将帮助我们即使移动根窗口,也能将{self.parent.bind("<Configure>", self.move_me)
添加到__init__
部分。在接下来,我们创建一个方法
move_me
,我们刚刚为它创建了一个绑定。 它的编写方式只有在self.image_window
不等于None
时才会生效,这应该可以防止使用self.image_window
时出现任何错误,但是我没有创建错误处理来处理用户关闭顶层窗口后发生的情况。这不难,但我想回答手头的主要问题。在以下是
move_me
方法:接下来,我们需要修改
on_enter
方法来创建顶层窗口,如果out class属性self.image_window
等于None
,如果它不等于None
,那么我们可以使用if语句的else部分来更新图像。在以下是修改后的
^{pr2}$on_enter
方法:尽管如此,您的代码中还有一些其他问题需要解决,但是这个答案应该会为您指明正确的方向。在
下面是您需要替换的代码部分:
有了这个:
相关问题 更多 >
编程相关推荐