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)
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. You can only use Tk()
once, all other windows have to be created using 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.)
Edit: your code has some other problems:
Tk
call and corresponding mainloop()
call. place()
, but it should be a last resort. Let tkinter calculate where to put things using pack()
and grid()
. Use Frames to make smaller parts that you can arrange. Using 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. from module import *
), it leads to bugs and it's against PEP8. 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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.