在 Tkinter 应用程序中调用函数时创建的重复帧

Duplicate Frames Created When Calling a Function in a Tkinter Application

所以这是我第一个 Python 使用 tkinter 的 GUI 项目。我来自 R.

我在查看文档后决定创建一个 class 来处理大部分工作。问题出现在我的增量函数 fwd() 和 bck() 中。如果我不在以下代码块中调用这些函数:

class App:
def __init__(self, master):
    ....
    self.total = 2
    self.fwd()
    self.bck()

整个代码的输出是一个空的 tkinter 框架。

另一方面,如果我确实调用它们,fwd() 函数会按预期工作,但每次我单击后退按钮 (command = bck()) 时,都会出现一个新的相同 GUI直接附在我当前 GUI 的底部。如果我再次单击后退按钮,将在当前 GUI 后面弹出另一个 GUI。

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 使用缩进作为其语法的一部分,我创建了一个函数 bck() 调用时,它包含整个 GUI 其余部分的代码。为了在指出这个问题后解决这个问题,我大量借鉴了: Python def function: How do you specify the end of the function?

您似乎有一个简单的缩进错误。看起来你打算让 bck 有四行代码,但因为几乎所有剩余的代码都缩进相同,所以它们都被认为是 bck 的一部分。