Python 当小部件被销毁时,新的小部件被放置在应用程序的一半位置

Python 当小部件被销毁时,新的小部件被放置在应用程序的一半位置,python,tkinter,Python,Tkinter,当我销毁第一个框架中的小部件时,我希望下一个框架中的下一个元素替换它们,但它们会在页面的中间移动。我不知道为什么会这样,知道吗?问题是关于var_state1,但我不确定。当我将run2放入for时,它只删除一个小部件,而不是所有小部件 from tkinter import PhotoImage from tkinter.ttk import * from time import strftime from tkinter import ttk import tkinter root = t

当我销毁第一个框架中的小部件时,我希望下一个框架中的下一个元素替换它们,但它们会在页面的中间移动。我不知道为什么会这样,知道吗?问题是关于var_state1,但我不确定。当我将run2放入for时,它只删除一个小部件,而不是所有小部件

from tkinter import PhotoImage
from tkinter.ttk import *
from time import strftime
from tkinter import ttk
import tkinter

root = tkinter.Tk()

varp = 0
btn_style=Style()
btn_style.configure('TButton', font =
               ('calibri', 20, 'bold'),
                    borderwidth = '4')

class Frame1(tkinter.Frame):
    def __init__(self, parent, *args, **kwargs):
        tkinter.Frame.__init__(self, parent, *args, **kwargs)


        self.Lab1 = Label(self, text="Check When Complete123:").grid(row=1, sticky=W)
        self.Lab2 = Checkbutton(self, text="Turn On Icecream Mashine", command=self.check_plus1).grid(row=2, sticky=W)
        self.Lab3 = Checkbutton(self, text="Turn On Radio", command=self.check_plus1).grid(row=3, sticky=W)
        self.Lab4 = Checkbutton(self, text="Turn On Oven", command=self.check_plus1).grid(row=4, sticky=W)
        self.Lab5 = ttk.Button(self, text="Done", command=self.var_state1, style="C.TButton").grid(row=5, sticky=W, pady=4)

    def check_plus1(self):
        global varp
        if varp <= 3:
            varp = varp + 1

    def var_state1(self):
        global varp
        if varp >= 3:
            print("it workd1")
            for widget in Frame1.winfo_children(self):
                    widget.destroy()
            run2()


class Frame2(tkinter.Frame):
    def __init__(self, parent, *args, **kwargs):
        tkinter.Frame.__init__(self, parent, *args, **kwargs)

        Label(self, text="Check When Complete:").grid(row=0, sticky=W)
        Checkbutton(self, text="Cleen The Tables", command=self.check_plus2).grid(row=1, sticky=W)
        Checkbutton(self, text="Fill The Sauces ex: Caramell, Hot Fudge...", command=self.check_plus2).grid(row=2, sticky=W)
        Checkbutton(self, text="Fill Spoons", command=self.check_plus2).grid(row=3, sticky=W)
        Checkbutton(self, text="Deep Clean The Bottem Of The Toppings Freaser", command=self.check_plus2).grid(row=4, sticky=W)
        Checkbutton(self, text="Fill The Toppings", command=self.check_plus2).grid(row=5, sticky=W)
        crl2 = ttk.Button(self, text="Done", command=self.var_state2, style="C.TButton").grid(row=6, sticky=W, pady=4)

    def check_plus2(self):
        global varp
        if varp <= 3:
            varp = varp + 1

    def var_state2(self):
        global varp
        if varp >= 5:
            print("it worked2")
            Close_Day()

def run1():
    MyFrame1 = Frame1(root)
    MyFrame1.pack(expand='true',fill='both')
    root.mainloop()
def run2():
    MyFrame2 = Frame2(root)
    MyFrame2.pack(expand='true', fill='both')
    root.mainloop()

run1()
从tkinter导入PhotoImage
从tkinter.ttk导入*
从时间导入strftime
从tkinter导入ttk
进口tkinter
root=tkinter.Tk()
varp=0
btn_style=style()
btn_样式。配置('TButton',字体=
('calibri',20,'bold'),
边框宽度='4')
类Frame1(tkinter.Frame):
定义初始化(自、父、*args、**kwargs):
tkinter.Frame.\uuuuu init\uuuuu(self,parent,*args,**kwargs)
self.Lab1=Label(self,text=“完成时检查123:”).grid(行=1,粘性=W)
self.Lab2=Checkbutton(self,text=“打开冰淇淋酱”,command=self.check\u plus1).grid(行=2,粘性=W)
self.Lab3=Checkbutton(self,text=“打开收音机”,command=self.check\u plus1)。grid(row=3,sticky=W)
self.Lab4=Checkbutton(self,text=“打开烤箱”,command=self.check\u plus1)。grid(row=4,sticky=W)
self.Lab5=ttk.Button(self,text=“Done”,command=self.var_state1,style=“C.TButton”).grid(row=5,sticky=W,pady=4)
def检查加1(自身):
全局varp
如果varp=3:
打印(“it工作1”)
对于Frame1.winfo_子项(self)中的小部件:
widget.destroy()
run2()
类Frame2(tkinter.Frame):
定义初始化(自、父、*args、**kwargs):
tkinter.Frame.\uuuuu init\uuuuu(self,parent,*args,**kwargs)
标签(self,text=“完成时检查:”).grid(行=0,粘性=W)
Checkbutton(self,text=“Cleen The Tables”,command=self.check\u plus2).grid(row=1,sticky=W)
检查按钮(self,text=“填充酱汁,例如焦糖、热软糖…”,命令=self.check\u plus2)。网格(行=2,粘性=W)
Checkbutton(self,text=“Fill Spoons”,command=self.check\u plus2).grid(row=3,sticky=W)
Checkbutton(self,text=“深层清洁配料的底部”,command=self.check\u plus2).网格(row=4,sticky=W)
检查按钮(self,text=“填充浇头”,command=self.check\u plus2).网格(row=5,sticky=W)
crl2=ttk.Button(self,text=“Done”,command=self.var_state2,style=“C.TButton”).grid(行=6,粘滞=W,pady=4)
def检查加2(自身):
全局varp
如果varp=5:
打印(“它工作了2”)
休馆日
def run1():
MyFrame1=Frame1(根目录)
MyFrame1.pack(expand='true',fill='both')
root.mainloop()
def run2():
MyFrame2=Frame2(根目录)
MyFrame2.pack(expand='true',fill='both')
root.mainloop()
run1()

此代码不会按发布的方式运行。请确保你发布的代码是可运行的。它为我运行。你有错误吗?我认为这个代码不可能为你工作。我得到错误
NameError:name“W”未定义,这是真的。您不定义它,也不导入它。它很容易修复,但您需要确保您发布的代码实际有效。