AttributeError:'_tkinter.tkapp'对象没有属性'choice'

时间:2019-04-15 09:16:21

标签: python-3.x tkinter

我用tkinter编写了一个GUI。我正在设计一个登录系统,它可以在管理员或安全团队之间进行选择。问题出在我设计的“选择”方法中。它是根据先前选择之间的选择来显示下一帧,但会失败。

错误是:

  

AttributeError:'_tkinter.tkapp'对象没有属性'choice'

注意:这不是完整的代码。

我的脚本:

import tkinter as tk
from tkinter import *
from tkinter import font  as tkfont 
import webbrowser

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")

    # the container is where we'll stack a bunch of frames
    # on top of each other, then the one we want visible
    # will be raised above the others
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (Main, TeamLogin, AdminLogin, Admin, SafetyTeam):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

        # put all of the pages in the same location;
        # the one on the top of the stacking order
        # will be the one that is visible.
        frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("Main")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()

    def get_page(self, classname):
        '''Returns an instance of a page given it's class name as a string'''
        for page in self.frames.values():
            if str(page.__class__.__name__) == classname:
                return page
        return None


    def loginS(self):
      u1 = userS.get()
      p1 = passS.get()
      username_entry1.delete(0, END)
      password_entry1.delete(0, END)
      list_of_files = os.listdir()
      if u1 in list_of_files:
          file1 = open(safetyteamfile, "r")
          verify = file1.read().splitlines()
          if p1 in verify:
            controller.show_frame("Safetyteam")
          else:
            tkMessageBox.showinfo("Error", "invalid username or password")

    def loginA(self):
      u2 = userA.get()
      p2 = passA.get()
      username_entry2.delete(0, END)
      password_entry2.delete(0, END)
      list_of_files = os.listdir()
      if u2 in list_of_files:
        file2 = open(adminfile, "r")
        verify1 = file2.read().splitlines()
        if p2 in verify1:
            pass
            controller.show_frame("Admin")
        else:
            tkMessageBox.showinfo("Error", "invalid username or password")

主代码:

class Main(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        global CheckVar1
        CheckVar1 = IntVar()
        L1 = tk.Label(self, text="Welcome to Smart Safety System", font=controller.title_font).pack(side="top", fill="x", pady=10)
        L2 = tk.Label(self, text="Log in as:", font=controller.title_font).pack()
        b1 = tk.Radiobutton(self, text="Safety Team", variable = CheckVar1, value=1)
        b2 = tk.Radiobutton(self, text="Admin", variable = CheckVar1, value=2) 
        b3 = tk.Button(self, text= "submit",command=lambda: controller.choice)
        b1.pack()
        b2.pack()
        b3.pack()

    def choice():
        if(CheckVar1.get()==1):
            self.show_frame("TeamLogin")

        if(CheckVar1.get()==2):
            self.show_frame("AdminLogin")

团队登录代码:

class TeamLogin(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        L1 = tk.Label(self, text="Safety Team Login", font=controller.title_font).pack(side="top", fill="x", pady=10)


        L2 = tk.Label(self,text="Enter username and password", font=controller.title_font).pack(side="top", fill="x", pady=20)
        global userS
        userS = StringVar()
        global passS
        passS = StringVar()


        L3 = tk.Label(self,text="username", font=controller.title_font).pack()
        e1 = tk.Entry(self, textvariable=userS).pack()
        L4 = Label(self,text="password", font=controller.title_font).pack()
        e2 = tk.Entry(self,textvariable=passS).pack()
        b1 = tk.Button(self, text = "Login", command=lambda: controller.loginS).pack()

安全小组代码:

class SafetyTeam(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

管理员登录代码:

class AdminLogin(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        L1 = tk.Label(self, text="Admin Login", font=controller.title_font).pack(side="top", fill="x", pady=10)

        L2 = tk.Label(self,text="Enter username and password", font=controller.title_font).pack(side="top", fill="x", pady=20)

        global userA
        userA = StringVar()
        global passA
        passA = StringVar()


        L3 = tk.Label(self,text="username", font=controller.title_font).pack()
        e1 = tk.Entry(self, textvariable=userA).pack()
        L4 = tk.Label(self,text="password", font=controller.title_font).pack()
        e2 = tk.Entry(self,textvariable=passA).pack()
        b1 = tk.Button(self, text = "Login", command=lambda: controller.loginA).pack()

主要管理代码:

class Admin(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

0 个答案:

没有答案