[英]Python Tkinter Label Update?
I searched a lot but i couldnt find the reason this code doesnt work. 我搜索了很多,但找不到该代码无法正常工作的原因。 It should calculate the time of the song by the entered bpm and bar but Label(Lb3) doesnt update by mins.set() command (choose upper button in the first menu)
它应该通过输入的bpm和bar计算歌曲的时间,但是Label(Lb3)不会通过mins.set()命令进行更新(选择第一个菜单中的上方按钮)
from tkinter import *
ana = Tk()
ana.title("Ana Menü")
ana.geometry("245x170")
def bpm():
def brtb():
equ = int(Eb1.get()) * 4 / int(Eb2.get())
str(equ)
mins.set(equ)
bar = Tk()
bar.title("Bar to BPM")
bar.geometry("245x150")
Lb1 = Label(bar, text="Bar:")
Lb2 = Label(bar, text="Bpm:")
Lb3 = Label(bar, textvariable=mins)
Eb1 = Entry(bar, width=10)
Eb2 = Entry(bar, width=10)
Bb1 = Button(bar, text="Calculate", command=brtb)
Lb1.place(relx=0.15, rely=.05)
Lb2.place(relx=0.12, rely=.2)
Lb3.place(relx=.25, rely=.5)
Eb1.place(relx=0.4, rely=.05)
Eb2.place(relx=0.4, rely=.2)
Bb1.place(relx=.3, rely=.4)
bar.update_idletasks()
bar.mainloop()
def minu():
print("hi")
mins=StringVar()
B1 = Button(ana, command=bpm, text="bar to min", width=123, height=5)
B1.pack()
B2 = Button(ana, command=minu, text="min to bar", width=122, height=5)
B2.pack()
ana.mainloop()
This is a symptom of using more than one Tk()
in your program. 这是在程序中使用多个
Tk()
的症状。 You can only use Tk()
once, all other windows have to be created using Toplevel()
. 您只能使用
Tk()
一次,所有其他窗口必须使用Toplevel()
创建。 Try this: 尝试这个:
bar = Toplevel()
(Technically you could use Tk()
more than once, but you really shouldn't; at least not until you have a good understanding of how it works.) (从技术上讲,您可以多次使用
Tk()
,但实际上不应该使用Tk()
;至少要等到对它的工作原理有了很好的了解之后,才能这样做。)
Edit: your code has some other problems: 编辑:您的代码还有其他一些问题:
Tk
call and corresponding mainloop()
call. Tk
调用和相应的mainloop()
调用。 place()
, but it should be a last resort. place()
很诱人,但这应该是不得已的方法。 Let tkinter calculate where to put things using pack()
and grid()
. pack()
和grid()
计算放置物品的位置。 Use Frames to make smaller parts that you can arrange. place
means a lot of work for you, and your code won't look the same on other computers, and it makes the code very hard to maintain. place
对您来说意味着很多工作,并且您的代码在其他计算机上看起来也不一样,这使得代码很难维护。 from module import *
), it leads to bugs and it's against PEP8. from module import *
),否则会导致错误,并且会违反PEP8。 update_idletasks()
. update_idletasks()
。 Here's how I would write it. 这就是我的写法。 Even if this seems way too advanced for you right now I recommend you use this pattern and get used to it;
即使现在看来这对您来说太高级了,我还是建议您使用这种模式并习惯它; it will help you a lot in the future.
将来会对您有很大帮助。
import tkinter as tk
class Bpm(tk.Toplevel):
def __init__(self, master=None, **kwargs):
tk.Toplevel.__init__(self, master, **kwargs)
self.title("Bar to BPM")
self.geometry("245x150")
self.mins=tk.StringVar()
entry_frame = tk.Frame(self)
Lb1 = tk.Label(entry_frame, text="Bar:")
Lb2 = tk.Label(entry_frame, text="Bpm:")
self.Eb1 = tk.Entry(entry_frame, width=10)
self.Eb2 = tk.Entry(entry_frame, width=10)
Bb1 = tk.Button(self, text="Calculate", command=self.brtb)
Lb3 = tk.Label(self, textvariable=self.mins)
Lb1.grid(row=0, column=0, sticky='e')
Lb2.grid(row=1, column=0, sticky='e')
self.Eb1.grid(row=0, column=1)
self.Eb2.grid(row=1, column=1)
entry_frame.pack()
Bb1.pack()
Lb3.pack()
def brtb(self):
equ = int(self.Eb1.get()) * 4 / int(self.Eb2.get())
self.mins.set(equ)
def minu():
print("hi")
def main():
ana = tk.Tk()
ana.title("Ana Menü")
ana.geometry("245x170")
B1 = tk.Button(ana, command=Bpm, text="bar to min", width=123, height=5)
B1.pack()
B2 = tk.Button(ana, command=minu, text="min to bar", width=122, height=5)
B2.pack()
ana.mainloop()
if __name__ == '__main__':
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.