Duplicate Frames Created When Calling a Function in a Tkinter Application
所以这是我第一个使用tkinter的PythonGUI项目。我来自R。
我在审阅文档后决定创建一个类来处理大部分工作。递增函数fwd()和bck()出现问题。如果我不在以下代码块中调用这些函数:
1 2 3 4 5 6 | class App: def __init__(self, master): .... self.total = 2 self.fwd() self.bck() |
整个代码的输出是一个空的tkinter帧。
另一方面,如果我确实调用了它们,fwd()函数的工作方式与我们预期的一样,但是每次我单击back按钮(command=bck())时,一个新的、相同的GUI将直接附加到我当前的GUI的底部。如果我再次单击后退按钮,另一个图形用户界面将在当前图形用户界面后面弹出。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | from tkinter import * from tkinter import font from tkinter import filedialog class App: #I'm not typing what goes in this class, this way I can avoid issues with App(Frame), etc. DUCKTYPE! def __init__(self, master): self.frame = Frame(master) self.frame.pack() self.master = master master.title("PyCCI Caste") self.total = 2 self.fwd() #Need to call these at the beginning otherwise the window is minimized??? No idea why. self.bck() #The back button creates a duplicate window... ## +Incrementer def fwd(self): self.total += 1 print(self.total) ## -Incrementer THIS CREATES A SECOND PANED WINDOW, WHY?! def bck(self): self.total += -1 if self.total < 3: self.total = 2 print(self.total) #Body self.k1 = PanedWindow(self.frame, #Note: if this is not self.frame, the error: 'App' object has no attribute 'tk' is thrown height=500, width=750, orient = VERTICAL) self.k1.pack(fill=BOTH, expand = 1) self.titlefont = font.Font(size = 12, weight = 'bold') self.boldfont = font.Font(size=8, weight = 'bold') self.textfont = font.Font(family = 'Arial', size = 10) #Title self.title = PanedWindow(self.k1) self.k1.add(self.title, padx = 10, pady = 10) Label(self.title, text ="Chronic Critically Ill Patient GUI", font = self.titlefont, fg="darkslateblue").pack() #Top row open csv window & button self.k2 = PanedWindow(self.k1) self.k1.add(self.k2) self.openbutton = Button(self.k2, text ="Open CSV")#, command = openfile()) self.openbutton.pack(side = LEFT, padx = 30) #Panes below buttons self.k3 = PanedWindow(self.k1) self.k1.add(self.k3) self.leftpane = PanedWindow(self.k3) self.k3.add(self.leftpane, width = 400, padx = 30, pady = 25, stretch ="first") self.separator = PanedWindow(self.k3, relief = SUNKEN) self.k3.add(self.separator, width=2, padx=1, pady=20) self.rightpane = PanedWindow(self.k3) self.k3.add(self.rightpane, width = 220, padx = 10, pady = 25, stretch ="never") #Left pane patient note text frame doo-diddly self.ptframe = LabelFrame(self.leftpane, text ="Medical Record", font = self.boldfont, padx = 0, pady=0, borderwidth = 0) self.ptframe.pack() Label(self.ptframe, text ="patient # of ##").pack() #Incrementer buttons self.buttonframe = Frame(self.ptframe) self.buttonframe.pack() self.buttonframe.place(relx=0.97, anchor = NE) #Back Button self.button1 = Button(self.buttonframe, text = 'Back', width = 6, command = self.bck) self.button1.grid(row = 0, column = 0, padx = 2, pady = 2) #Next Button self.button2 = Button(self.buttonframe, text = 'Next', width = 6, command = self.fwd) self.button2.grid(row = 0, column = 2, padx = 2, pady = 2) #Scrollbar! self.ptscroll = Scrollbar(self.ptframe) self.ptscroll.pack(side = RIGHT, fill = Y) self.pttext = Text(self.ptframe, height=300, width=400, wrap=WORD, font=self.textfont, spacing1=2, spacing2=2, spacing3=3, padx=15, pady=15) self.pttext.pack() self.ptscroll.config(command=self.pttext.yview) self.pttext.config(yscrollcommand=self.ptscroll.set) #Checkbuttons self.checkframe = LabelFrame(self.rightpane, text="Indicators", font=self.boldfont, padx = 10, pady = 10, borderwidth=0) self.checkframe.pack() self.check1 = Checkbutton(self.checkframe, text="Non-Adherence") self.check1.grid(row = 1, column = 0, sticky = W) root = Tk() app = App(root) ## apply the class"App" to Tk() ### Menu stuff does not need to be part of the class menubar = Menu(root) filemenu = Menu(menubar, tearoff=0) filemenu.add_command(label="Open CSV")#, command=openfile) menubar.add_cascade(label="File", menu=filemenu) helpmenu = Menu(menubar, tearoff=0) helpmenu.add_command(label="About")#, command=about) menubar.add_cascade(label="Help", menu=helpmenu) root.config(menu=menubar) root.mainloop() |
你们觉得怎么样?如果我这里没有任何相关信息,请告诉我。我遇到的困难是,我还不知道关于python/tkinter我还不知道什么。
谢谢,我真的很欣赏你的洞察力和方向。
解决方法(感谢Bryan Oakley&Tigerhawkt3):由于Python使用缩进作为其语法的一部分,我创建了一个函数
您似乎有一个简单的缩进错误。似乎您打算让