这是我第一次尝试使用GUI和python,所以任何指导都会受到赞赏。我写了一个包含在client.py中的tkinter GUI,我希望先显示它。成功登录后,将出现第二个gui,它包含在menu.py中。我无法理解为什么在登录后menu.py的gui无法正常显示。我尝试从Menu.py获取所有内容并将它们放在Client.py中,但这似乎无法解决问题。
会发生什么: 1.)登录GUI出现 2.)我登录w /正确的凭据 3.)我得到200 OK消息 4.)出现空GUI,我收到此错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Pat\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "C:\Users\Pat\Documents\GitHub\DunderMifflinSales\DunderMifflinClient.py", line 51, in _login_btn_clicked
mf = Menu.MenuFrame(menu)
File "C:\Users\Pat\Documents\GitHub\DunderMifflinSales\Menu.py", line 9, in __init__
mainframe = ttk.Frame(root, padding="3 3 12 12")
NameError: name 'root' is not defined
打算如下: 1.)登录GUI出现 2.)使用正确的凭据登录 3.)菜单gui出现
我很难理解为什么我的逻辑不起作用。感谢您的任何帮助。
Client.py
import socket
from tkinter import *
import tkinter.messagebox as tm
from tkinter import ttk
import Menu
serverName = 'localhost'
serverPort = 12000
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
class LoginFrame(Frame):
def __init__(self, master):
super().__init__(master)
self.label_1 = Label(self, text="Username")
self.label_2 = Label(self, text="Password")
self.entry_1 = Entry(self)
self.entry_2 = Entry(self, show="*")
self.label_1.grid(row=0, sticky=E)
self.label_2.grid(row=1, sticky=E)
self.entry_1.grid(row=0, column=1)
self.entry_2.grid(row=1, column=1)
self.logbtn = Button(self, text="Login", command = self._login_btn_clicked)
self.logbtn.grid(columnspan=2)
self.pack()
def _login_btn_clicked(self):
username = self.entry_1.get()
clientSocket.sendto(username.encode('UTF-8'),(serverName, serverPort))
password = self.entry_2.get()
clientSocket.sendto(password.encode('UTF-8'),(serverName, serverPort))
message, address = clientSocket.recvfrom(1024)
message = message.decode('UTF-8')
if message == "200 OK":
tm.showinfo("Logged in", message)
menu = Tk()
menu.title("Dunder Mifflin GUI")
mf = Menu.MenuFrame(menu)
menu.mainloop()
else:
tm.showinfo("Login error", message)
def main():
root = Tk()
root.title("Dunder Mifflin GUI")
root.geometry('{}x{}'.format(270, 80))
lf = LoginFrame(root)
root.mainloop()
if __name__ == "__main__":
main()
Menu.py:
from tkinter import *
from tkinter import ttk
class MenuFrame(Frame):
def __init__(self, master):
super().__init__(master)
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column = 0, row = 0, sticky = (N, W, E, S))
mainframe.columnconfigure(0, weight = 1)
mainframe.rowconfigure(0, weight = 1)
name = "John"
sales = StringVar()
moreSales = StringVar()
sales.set("1")
welcome = "Welcome, " + name + "!"
sales_entry = ttk.Entry(mainframe, width = 7, textvariable = moreSales)
ttk.Label(mainframe, text = welcome).grid(column = 2, row = 1, sticky = W)
ttk.Label(mainframe, text = "Current Sales:").grid(column = 1, row = 2, sticky = W)
ttk.Label(mainframe, textvariable = sales).grid(column = 2, row = 2, sticky = (E))
ttk.Label(mainframe, text = "sales").grid(column = 3, row = 2, sticky = W)
ttk.Label(mainframe, text = "Add Sales").grid(column = 1, row = 3, sticky = W)
sales_entry.grid(column = 2, row = 3, sticky = (W, E))
ttk.Button(mainframe, text = "Add", command = MenuFrame.add(sales, moreSales)).grid(column = 3, row = 3, sticky = W)
ttk.Button(mainframe, text = "Log Out", command = MenuFrame.logout).grid(column = 2, row = 4, sticky = W)
def add(sales, moreSales):
try:
value1 = int(sales.get())
value2 = int(moreSales.get())
sales.set(value1 + value2)
except ValueError:
pass
def logout():
root.destroy()