[英]Blank 'tk' window popping up when program is ran from a button which runs an imported program | For Tkinter
So I have this launch program which I want it to an imported program and simultaneously destroy the launch program.所以我有这个启动程序,我希望它是一个导入的程序并同时销毁启动程序。 However, when I do this, the imported program runs but also a blank 'tk' window pops up along the program.
但是,当我这样做时,导入的程序会运行,但还会弹出一个空白的 'tk' window 程序。 Would anyone know how to solve this issue?
有人知道如何解决这个问题吗?
This is the code for the launch program:这是启动程序的代码:
from tkinter import *
from stopwatch import Main
root = Tk()
def launch():
root.destroy()
Main()
launchbutton = Button(root, text='Launch Program', command=launch)
launchbutton.pack()
root.mainloop()
This is the code for the imported module:这是导入模块的代码:
from tkinter import *
import time
def Main():
global root
root = Toplevel()
width = 600
height = 200
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width / 2) - (width / 2)
y = (screen_height / 2) - (height / 2)
root.geometry("%dx%d+%d+%d" % (width, height, x, y))
root.title("Stopwatch")
Top = Frame(root, width=600)
Top.pack(side=TOP)
stopWatch = StopWatch(root)
stopWatch.pack(side=TOP)
Bottom = Frame(root, width=600)
Bottom.pack(side=BOTTOM)
Start = Button(Bottom, text='Start', command=stopWatch.Start, width=10, height=2)
Start.pack(side=LEFT)
Stop = Button(Bottom, text='Stop', command=stopWatch.Stop, width=10, height=2)
Stop.pack(side=LEFT)
Reset = Button(Bottom, text='Reset', command=stopWatch.Reset, width=10, height=2)
Reset.pack(side=LEFT)
Exit = Button(Bottom, text='Close', command=stopWatch.Exit, width=10, height=2)
Exit.pack(side=LEFT)
Title = Label(Top, text="Stopwatch", font=("arial", 18), fg="white", bg="black")
Title.pack(fill=X)
root.config(bg="black")
class StopWatch(Frame):
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent, kw)
self.startTime = 0.0
self.nextTime = 0.0
self.onRunning = 0
self.timestr = StringVar()
self.MakeWidget()
def MakeWidget(self):
timeText = Label(self, textvariable=self.timestr, font=("times new roman", 50), fg="green", bg="black")
self.SetTime(self.nextTime)
timeText.pack(fill=X, expand=NO, pady=2, padx=2)
def Updater(self):
self.nextTime = time.time() - self.startTime
self.SetTime(self.nextTime)
self.timer = self.after(50, self.Updater)
def SetTime(self, nextElap):
minutes = int(nextElap / 60)
seconds = int(nextElap - minutes * 60.0)
miliSeconds = int((nextElap - minutes * 60.0 - seconds) * 100)
self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, miliSeconds))
def Start(self):
if not self.onRunning:
self.startTime = time.time() - self.nextTime
self.Updater()
self.onRunning = 1
def Stop(self):
if self.onRunning:
self.after_cancel(self.timer)
self.nextTime = time.time() - self.startTime
self.SetTime(self.nextTime)
self.onRunning = 0
def Exit(self):
root.destroy()
exit()
def Reset(self):
self.startTime = time.time()
self.nextTime = 0.0
self.SetTime(self.nextTime)
if __name__ == '__main__':
Main()
Thank you:)谢谢:)
You have incorrectly made the stopwatch window as Toplevel().您错误地将秒表 window 设置为 Toplevel()。 This causes the the Tk() window to open.
这会导致 Tk() window 打开。 So when you use Toplevel(), a Tk() window should also exist.
所以当你使用 Toplevel() 时,一个 Tk() window 也应该存在。
Corrected code of stopwatch:秒表的更正代码:
from tkinter import *
import time
def Main():
global root
root = Tk()
width = 600
height = 200
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width / 2) - (width / 2)
y = (screen_height / 2) - (height / 2)
root.geometry("%dx%d+%d+%d" % (width, height, x, y))
root.title("Stopwatch")
Top = Frame(root, width=600)
Top.pack(side=TOP)
stopWatch = StopWatch(root)
stopWatch.pack(side=TOP)
Bottom = Frame(root, width=600)
Bottom.pack(side=BOTTOM)
Start = Button(Bottom, text='Start', command=stopWatch.Start, width=10, height=2)
Start.pack(side=LEFT)
Stop = Button(Bottom, text='Stop', command=stopWatch.Stop, width=10, height=2)
Stop.pack(side=LEFT)
Reset = Button(Bottom, text='Reset', command=stopWatch.Reset, width=10, height=2)
Reset.pack(side=LEFT)
Exit = Button(Bottom, text='Close', command=stopWatch.Exit, width=10, height=2)
Exit.pack(side=LEFT)
Title = Label(Top, text="Stopwatch", font=("arial", 18), fg="white", bg="black")
Title.pack(fill=X)
root.config(bg="black")
class StopWatch(Frame):
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent, kw)
self.startTime = 0.0
self.nextTime = 0.0
self.onRunning = 0
self.timestr = StringVar()
self.MakeWidget()
def MakeWidget(self):
timeText = Label(self, textvariable=self.timestr, font=("times new roman", 50), fg="green", bg="black")
self.SetTime(self.nextTime)
timeText.pack(fill=X, expand=NO, pady=2, padx=2)
def Updater(self):
self.nextTime = time.time() - self.startTime
self.SetTime(self.nextTime)
self.timer = self.after(50, self.Updater)
def SetTime(self, nextElap):
minutes = int(nextElap / 60)
seconds = int(nextElap - minutes * 60.0)
miliSeconds = int((nextElap - minutes * 60.0 - seconds) * 100)
self.timestr.set('%02d:%02d:%02d' % (minutes, seconds, miliSeconds))
def Start(self):
if not self.onRunning:
self.startTime = time.time() - self.nextTime
self.Updater()
self.onRunning = 1
def Stop(self):
if self.onRunning:
self.after_cancel(self.timer)
self.nextTime = time.time() - self.startTime
self.SetTime(self.nextTime)
self.onRunning = 0
def Exit(self):
root.destroy()
exit()
def Reset(self):
self.startTime = time.time()
self.nextTime = 0.0
self.SetTime(self.nextTime)
if __name__ == '__main__':
Main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.