[英]Python 2.71.11 Tkinter window not closing “Application not Responding”
[英]Pararell instance of tkinter application window python
我想创建一些简单的tkinter python应用程序(如Windows上的StickyNotes),我创建了类mainApplication ,我不知道如何通过简单地触发按钮来创建该类的另一个实例,该实例将与其他窗口平行显示(甚至多个窗口)。 我知道如何将功能分配给pushButton和其他简单的东西,但是问题在于显示此Pararell窗口。 在此先感谢您的帮助。
class mainApplication(Frame):
_ids = count(0)
def __init__(self, parent):
""" """
self.id = next(self._ids)
Frame.__init__(self, parent)
self.parent = parent
self.parent.minsize(width=200,height=100)
self.parent.geometry(('%dx%d+%d+%d' % (200, 100, 1700, 0+self.id*100)))
self.initUI()
def initUI(self):
""" """
self.parent.title("a2l")
self.pack(fill=BOTH, expand=True)
style = Style()
style.configure("TFrame", background="#333")
frame1 = Frame(self, style="TFrame")
frame1.pack(fill=X)
self.lbl0 = Label(frame1, text="api", width=7, background="#333", foreground = "red")
self.lbl0.pack(side=TOP, padx=5, pady=5)
self.closeButton = Button(self, text="new", command = self.createNewInstance)
self.closeButton.pack(side=RIGHT, padx=5, pady=5)
#=======================================================================
# self.generateButton = Button(self, text="GENERATE", command = self.)
# self.generateButton.pack(side=RIGHT, padx=5, pady=5)
#=======================================================================
def createNewInstance(self):
y = mainApplication()
return y
if __name__ == "__main__":
root = Tk()
x = mainApplication(root).pack(side="top", expand=False)
Tk().mainloop()
使用tkinter在一个应用程序中不应创建多个Tk()
窗口。 相反,tkinter提供了一个名为Toplevel
的小部件,可用于此类事情。
它创建了另一个窗口,该窗口可以存在于Tk()
窗口旁边以及其他Toplevel
控件旁边。
您可以使用它来创建一系列持久性窗口,并通过任何触发方式(包括Button
在其上想要的任何文本或小部件。
请参阅下面的示例进行演示:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.top = [] #list to contain the Toplevel widgets
self.entry = Entry(self.root)
self.button = Button(self.root, text="Create window", command=self.command)
self.entry.pack()
self.button.pack()
def command(self): #called when button is pressed
self.top.append(Toplevel(self.root)) #adds new Toplevel to the list
Label(self.top[len(self.top)-1], text=self.entry.get()).pack() #Adds label equal to the entry widget to the new toplevel
root = Tk()
App(root)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.