我对Tkinter很新。我在Tkinter制作了这个类似“Hello World”的GUI程序。但是,每次单击退出按钮时,程序都会崩溃。提前谢谢!
from Tkinter import *
import sys
class Application(Frame):
def __init__(self,master=None):
Frame.__init__(self,master=None)
self.grid()
self.createWidgets()
def createWidgets(self):
self.quitButton = Button(text='Quit',command=self.quit)#Problem here
self.quitButton.grid()
app = Application()
app.master.title("Sample application")
app.mainloop()
答案 0 :(得分:3)
在Tkinter中,根元素是Tk
个对象。 Application
应该是Tk
的子类,而不是Frame
:
from Tkinter import *
import sys
class Application(Tk):
def __init__(self):
Tk.__init__(self)
self.grid()
self.createWidgets()
def createWidgets(self):
self.quitButton = Button(text='Quit',command=self.destroy) # Use destroy instead of quit
self.quitButton.grid()
app = Application()
app.title("Sample application")
app.mainloop()
答案 1 :(得分:0)
此守则现在运作良好:
import tkinter
class MyApp(tkinter.LabelFrame):
def __init__(self, master=None):
super().__init__(master, text="Hallo")
self.pack(expand=1, fill="both")
self.createWidgets()
self.createBindings()
def createWidgets(self):
self.label = tkinter.Label(self)
self.label.pack()
self.label["text"] = "Bitte sende ein Event"
self.entry = tkinter.Entry(self)
self.entry.pack()
self.ok = tkinter.Button(self)
self.ok.pack()
self.ok["text"] = "Beenden"
self.ok["command"] = self.master.destroy
def createBindings(self):
self.entry.bind("Entenhausen", self.eventEntenhausen)
self.entry.bind("<ButtonPress-1>", self.eventMouseClick)
self.entry.bind("<MouseWheel>", self.eventMouseWheel)
def eventEntenhausen(self, event):
self.label["text"] = "Sie kennen das geheime Passwort!"
def eventMouseClick(self, event):
self.label["text"] = "Mausklick an Position " \
"({},{})".format(event.x, event.y)
def eventMouseWheel(self, event):
if event.delta < 0:
self.label["text"] = "Bitte bewegen Sie das Mausrad"\
" in die richtige Richtung."
else:
self.label["text"] = "Vielen Dank!"
root = tkinter.Tk()
app = MyApp(root)
app.mainloop()
答案 2 :(得分:0)
使用AmazonS3Client client = new AmazonS3Client();
ListBucketsResponse response = client.ListBuckets();
Console.WriteLine("Canonical user ID - {0}", response.Owner.Id);
时,python解释器关闭,而tkinter应用程序关闭。因此,请尝试self.quit()
命令并在.destroy()
之后使用.mainloop()
。希望这会有所帮助。
答案 3 :(得分:0)
您难以使用__init__
。这样做:
from tkinter import *
root = Tk()
btn_quit = Button(root, text='Quit', command=quit()).pack()
root.mainloop()
如果你执行self.quit
,那就是quit命令,这样东西就会崩溃!
希望这有帮助!
答案 4 :(得分:0)
尝试使用raise SystemExit
这可能更好。
或查看me