Tkinter 无法正确关闭并启动新文件

Tkinter won't close correctly and launch new file

我做了一个小登录界面。输入凭据后,我希望关闭 Tkinter 并打开一个新的 python 文件。如果你执行下面的代码,它会给我错误 NameError: name ttk is not defined。在另一个文件中,我导入了所有内容并为其指定了正确的名称。

我用于登录的文件:

from tkinter import *
import tkinter.messagebox as tm

class LoginFrame(Frame):
    def __init__(self, master):
        super().__init__(master)

        self.label_username = Label(self, text="Username")
        self.label_password = Label(self, text="Password")
        self.photo = PhotoImage(file="sbshreg.png")

        self.label_image = Label(root, image=self.photo)
        self.label_image.image = self.photo

        self.entry_username = Entry(self)
        self.entry_password = Entry(self, show="*")

        self.label_username.grid(row=0, sticky=E)
        self.label_password.grid(row=1, sticky=E)
        self.label_image.grid(row=3, column=2, rowspan=2, columnspan=2, sticky=W, padx=10)

        self.entry_username.grid(row=0, column=1, sticky=E)
        self.entry_password.grid(row=1, column=1, sticky=E)

        self.logbtn = Button(self, text="Login", command=self._login_btn_clicked)
        self.logbtn.grid(columnspan=2, column=1, row=2, sticky=S+E+N+W)

        self.grid()

    def _login_btn_clicked(self):
        username = self.entry_username.get()
        password = self.entry_password.get()

        if username == "123" and password == "123":
            tm.showinfo("SBSHREG", "Welcome 123")
            #The sweet spot where all goes wrong...
            self.destroy()
            exec(open("./BatchFrame.py").read())
        else:
            tm.showerror("SBSHREG", "Incorrect username")

root = Tk()
root.title("SBSHREG")
root.geometry("235x120")
lf = LoginFrame(root)
root.mainloop()

在另一个文件中我得到了这个:from tkinter import ttk as ttk 这应该可以防止另一个文件中的错误发生。

from tkinter import *
import tkinter.messagebox as tm
from tkinter import ttk as ttk

class BatchFrame(Frame):
    def __init__(self, master):
        super().__init__(master)

        self.photo = PhotoImage(file="sbshreg.png")
        self.label_photo = Label(root, image=self.photo)
        self.label_photo.image = self.photo

        self.label_photo.grid(row=0, column=2, sticky=N, padx=10, pady=10)

        #Add frame starting here
        frame = LabelFrame(self.master, text='Voeg batch toe')
        frame.grid (row=0, column=0, padx=10)

        self.label_batch = Label(frame, text="Batchnummer")
        self.label_employee = Label(frame, text="Medewerker")
        self.label_material = Label(frame, text="Materiaalsoort")
        self.label_weight = Label(frame, text="Gewicht")

        self.entry_batch = Entry(frame)
        self.entry_employee = Entry(frame)
        self.entry_material= Entry(frame)
        self.entry_weight = Entry(frame)

        self.label_batch.grid(row=0, column=0, sticky=S+E+N+W, columnspan=2, padx=10)
        self.label_employee.grid(row=2, column=0,  sticky=S+E+N+W, columnspan=2, padx=10)
        self.label_material.grid(row=4, column=0,  sticky=S+E+N+W, columnspan=2, padx=10)
        self.label_weight.grid(row=6, column=0,  sticky=S+E+N+W, columnspan=2, padx=10)

        self.entry_batch.grid(row=1, column=0, sticky=S+E+N+W, columnspan=2, padx=10)
        self.entry_employee.grid(row=3, column=0, sticky=S+E+N+W, columnspan=2, padx=10)
        self.entry_material.grid(row=5, column=0, sticky=S+E+N+W, columnspan=2, padx=10)
        self.entry_weight.grid(row=7, column=0, sticky=S+E+N+W, columnspan=2, padx=10)

        self.btnadd = Button(frame, text='Voeg toe', command=self._btn_add_clicked)
        self.btnadd.grid(column=0, row=8, pady=10)

        #Search frame starting here
        framesearch = LabelFrame(self.master, text='Zoek')
        framesearch.grid(row=0, column=1, sticky=N)

        self.label_batch = Label(framesearch, text="Batchnummer")
        self.label_employee = Label(framesearch, text="Medewerker")

        self.entry_batch = Entry(framesearch)
        self.entry_employee = Entry(framesearch)

        self.label_batch.grid(row=0, column=0, sticky=S, columnspan=2, padx=10)
        self.label_employee.grid(row=2, column=0, sticky=S, columnspan=2, padx=10)

        self.entry_batch.grid(row=1, column=0, sticky=S + E + N + W, columnspan=2, padx=10, pady=10)
        self.entry_employee.grid(row=3, column=0, sticky=S + E + N + W, columnspan=2, padx=10, pady=10)

        self.btnbatch = Button(framesearch, text="Zoek", command=self._btn_batch_clicked)
        self.btnemployee = Button(framesearch, text="Zoek", command=self._btn_employee_clicked)

        self.btnbatch.grid(columnspan=1, column=2, row=1, sticky=W, padx=10)
        self.btnemployee.grid(columnspan=1, column=2, row=3, sticky=W, padx=10)

        #This is the viewingarea for the data
        self.tree = ttk.Treeview (height=10, columns=("Batchnummer", "Medewerker", "Materiaalsoort", "Gewicht"))
        self.tree.grid (row=9, columnspan=10, padx=10, pady=10)
        self.tree.heading('#1', text='Batchnummer', anchor=W)
        self.tree.heading('#2', text='Medewerker', anchor=W)
        self.tree.heading('#3', text='Materiaalsoort', anchor=W)
        self.tree.heading('#4', text='Gewicht', anchor=W)
        self.tree.column('#0', stretch=NO, minwidth=0, width=0)
        self.tree.column('#1', stretch=NO, minwidth=0, width=100)
        self.tree.column('#2', stretch=NO, minwidth=0, width=100)
        self.tree.column('#3', stretch=NO, minwidth=0, width=100)
        self.tree.column('#4', stretch=NO, minwidth=0, width=100)

        self.grid()

    def _btn_add_clicked(self):
        batch = self.entry_batch.get()

    def _btn_batch_clicked(self):
        batch = self.entry_batch.get()

    def _btn_employee_clicked(self):
        batch = self.entry_employee.get()


root = Tk()
root.title("SBSHREG")
root.geometry("432x480")
lf = BatchFrame(root)
root.mainloop()

如果我将 self.destroy() 更改为 root.destroy(),我会收到以下错误:_tkinter.TclError: can't invoke "label" command: application has been destroyed。在第二个文件中,函数尚未完成,因为我仍在处理该文件,但这应该不会对错误产生任何影响。

我到处找,试了很多,还是一点头绪都没有...

我建议导入 batchframe 而不是执行它。

from tkinter import *
import tkinter.messagebox as tm
from tkinter import ttk as ttk
from batchframe import BatchFrame

class LoginFrame(Frame):
    def __init__(self, master):
        super().__init__(master)
        self.master = master

        self.label_username = Label(self, text="Username")
        self.label_password = Label(self, text="Password")
        self.photo = PhotoImage(file="icon.png")

        self.label_image = Label(root, image=self.photo)
        self.label_image.image = self.photo

        self.entry_username = Entry(self)
        self.entry_password = Entry(self, show="*")

        self.label_username.grid(row=0, sticky=E)
        self.label_password.grid(row=1, sticky=E)
        self.label_image.grid(row=3, column=2, rowspan=2, columnspan=2, sticky=W, padx=10)

        self.entry_username.grid(row=0, column=1, sticky=E)
        self.entry_password.grid(row=1, column=1, sticky=E)

        self.logbtn = Button(self, text="Login", command=self._login_btn_clicked)
        self.logbtn.grid(columnspan=2, column=1, row=2, sticky=S+E+N+W)

        self.grid()

    def _login_btn_clicked(self):
        username = self.entry_username.get()
        password = self.entry_password.get()

        if username == "123" and password == "123":
            tm.showinfo("SBSHREG", "Welcome 123")
            #The sweet spot where all goes wrong...
            self.destroy()
            # Create the instance of the BatchFrame class, passing in self.master
            self.batchframe = BatchFrame(self.master)
        else:
            tm.showerror("SBSHREG", "Incorrect username")

然后在 batchframe.py 中将变量引用从 root 更改为 master

我不同意您调用第二个 python 文件的方法。 你真的不需要在这里使用 exec。

但是,如果您真的想要,则需要将 import tkinter.ttk as ttk 添加到您的第一页才能正常工作。

您最好的选择是将第二个文件导入第一个文件并通过调用 class 名称来调用它。

因此您需要在第一页上导入 import BatchFrame

然后在你的代码中调用它。

替换:

exec(open("./test2.py").read())

与:

BatchFrame.BatchFrame(root)

我确实在你的 BatchFrame class 想法中看到一个错误。

改变这个:

self.label_photo = Label(root, image=self.photo)

对此:

self.label_photo = Label(master, image=self.photo)

更新: 要通过评论解决您的问题,请进行以下更改:

1:将其添加到您的 BatchFrame Class:

class BatchFrame(Frame):
    def __init__(self, master):
        super().__init__(master)
        self.master = master # add this
        self.master.title("SBSHREG") # and this
        self.master.geometry("432x480") # and this

2:从您的 BatchFrame 文件中删除它:

root = Tk()
root.title("SBSHREG")
root.geometry("432x480")
lf = BatchFrame(root)
root.mainloop()

3:将此添加到您的 LoginFrame class:

class LoginFrame(Frame):
    def __init__(self, master):
        super().__init__(master)
        self.master = master # add this

4:在 LoginFrame class 中更改为:

BatchFrame.BatchFrame(root)

对此:

BatchFrame.BatchFrame(self.master)

考虑在顶层 class、GUI 中初始化框架,它处理两个框架的打开,其中 LoginFrame 调用其父级的 open_batch()(现在 lambda 实施)方法。下面假设 LoginFrame.pyBatchFrame.pyGUI_App[= 位于同一文件夹中34=] 脚本。

这样,脚本 运行 作为一个 Tk() 实例上的独立模块。

GUIApp (调用子框架、LoginFrame 和 BatchFrame)

from tkinter import *
import LoginFrame as LF
import BatchFrame as BF

class GUI():

    def __init__(self):    
        self.root = Tk()
        self.root.title("SBSHREG")
        self.root.geometry("235x120")

        self.root.open_batch = self.open_batch

        lf = LF.LoginFrame(self.root)
        self.root.mainloop()

    def open_batch(self):
        bf = BF.BatchFrame(self.root)

app = GUI()

LoginFrame

from tkinter import *
import tkinter.messagebox as tm

class LoginFrame(Frame):

    def __init__(self, master):
        super().__init__(master)

        self.label_username = Label(self, text="Username")
        self.label_password = Label(self, text="Password")
        self.photo = PhotoImage(file="sbshreg.png")

        self.label_image = Label(self, image=self.photo)
        self.label_image.image = self.photo

        self.entry_username = Entry(self)
        self.entry_password = Entry(self, show="*")

        self.label_image.grid(row=0, column=2, rowspan=2, columnspan=2, sticky=W, padx=10)
        self.label_username.grid(row=2, sticky=E)
        self.label_password.grid(row=3, sticky=E)

        self.entry_username.grid(row=2, column=1, sticky=E)
        self.entry_password.grid(row=3, column=1, sticky=E)

        self.logbtn = Button(self, text="Login", command=lambda: self._login_btn_clicked(master))
        self.logbtn.grid(row=4, column=1, columnspan=2, sticky=S+E+N+W)

        self.grid()

    def _login_btn_clicked(self, controller):
        username = self.entry_username.get()
        password = self.entry_password.get()

        if username == "123" and password == "123":
            tm.showinfo("SBSHREG", "Welcome 123")

            self.destroy()
            controller.open_batch()
        else:
            tm.showerror("SBSHREG", "Incorrect username")

BatchFrame

from tkinter import *
import tkinter.messagebox as tm
from tkinter import ttk as ttk

class BatchFrame(Frame):

    def __init__(self, master):
        super().__init__(master)    

        self.photo = PhotoImage(file="sbshreg.png")
        self.label_photo = Label(master, image=self.photo)
        self.label_photo.image = self.photo

        self.label_photo.grid(row=0, column=2, sticky=N, padx=10, pady=10)

        #Add frame starting here
        frame = LabelFrame(master, text='Voeg batch toe')
        frame.grid (row=0, column=0, padx=10)

        self.label_batch = Label(frame, text="Batchnummer")
        self.label_employee = Label(frame, text="Medewerker")
        self.label_material = Label(frame, text="Materiaalsoort")
        self.label_weight = Label(frame, text="Gewicht")

        self.entry_batch = Entry(frame)
        self.entry_employee = Entry(frame)
        self.entry_material= Entry(frame)
        self.entry_weight = Entry(frame)

        self.label_batch.grid(row=0, column=0, sticky=S+E+N+W, columnspan=2, padx=10)
        self.label_employee.grid(row=2, column=0,  sticky=S+E+N+W, columnspan=2, padx=10)
        self.label_material.grid(row=4, column=0,  sticky=S+E+N+W, columnspan=2, padx=10)
        self.label_weight.grid(row=6, column=0,  sticky=S+E+N+W, columnspan=2, padx=10)

        self.entry_batch.grid(row=1, column=0, sticky=S+E+N+W, columnspan=2, padx=10)
        self.entry_employee.grid(row=3, column=0, sticky=S+E+N+W, columnspan=2, padx=10)
        self.entry_material.grid(row=5, column=0, sticky=S+E+N+W, columnspan=2, padx=10)
        self.entry_weight.grid(row=7, column=0, sticky=S+E+N+W, columnspan=2, padx=10)

        self.btnadd = Button(frame, text='Voeg toe', command=self._btn_add_clicked)
        self.btnadd.grid(column=0, row=8, pady=10)

        #Search frame starting here
        framesearch = LabelFrame(master, text='Zoek')
        framesearch.grid(row=0, column=1, sticky=N)

        self.label_batch = Label(framesearch, text="Batchnummer")
        self.label_employee = Label(framesearch, text="Medewerker")

        self.entry_batch = Entry(framesearch)
        self.entry_employee = Entry(framesearch)

        self.label_batch.grid(row=0, column=0, sticky=S, columnspan=2, padx=10)
        self.label_employee.grid(row=2, column=0, sticky=S, columnspan=2, padx=10)

        self.entry_batch.grid(row=1, column=0, sticky=S + E + N + W, columnspan=2, padx=10, pady=10)
        self.entry_employee.grid(row=3, column=0, sticky=S + E + N + W, columnspan=2, padx=10, pady=10)

        self.btnbatch = Button(framesearch, text="Zoek", command=self._btn_batch_clicked)
        self.btnemployee = Button(framesearch, text="Zoek", command=self._btn_employee_clicked)

        self.btnbatch.grid(columnspan=1, column=2, row=1, sticky=W, padx=10)
        self.btnemployee.grid(columnspan=1, column=2, row=3, sticky=W, padx=10)

        #This is the viewingarea for the data
        self.tree = ttk.Treeview (height=10, columns=("Batchnummer", "Medewerker", "Materiaalsoort", "Gewicht"))
        self.tree.grid (row=9, columnspan=10, padx=10, pady=10)
        self.tree.heading('#1', text='Batchnummer', anchor=W)
        self.tree.heading('#2', text='Medewerker', anchor=W)
        self.tree.heading('#3', text='Materiaalsoort', anchor=W)
        self.tree.heading('#4', text='Gewicht', anchor=W)
        self.tree.column('#0', stretch=NO, minwidth=0, width=0)
        self.tree.column('#1', stretch=NO, minwidth=0, width=100)
        self.tree.column('#2', stretch=NO, minwidth=0, width=100)
        self.tree.column('#3', stretch=NO, minwidth=0, width=100)
        self.tree.column('#4', stretch=NO, minwidth=0, width=100)

        self.grid()

    def _btn_add_clicked(self):
        batch = self.entry_batch.get()

    def _btn_batch_clicked(self):
        batch = self.entry_batch.get()

    def _btn_employee_clicked(self):
        batch = self.entry_employee.get()