我是Python新手并尝试设计GUI应用程序。我在python项目的目录中有3个类,每个类都投影不同大小的不同窗口因此我使用 window.destroy(每次在我调用另一个类之前每次都有,但是在一个窗口中,我有一个后退按钮,但在尝试访问Menu()函数时,会出现一个空窗口。以下是所有3个类登录,菜单和customerDetails的代码。正在遵循的路径:登录 - >菜单 - >新客户 - >后退按钮 - >(理想结果=菜单,结果=空白窗口)。
from tkinter import *
from tkinter import ttk
from frontend.menu import Menu
class Login:
def log(self):
if self.username.get() == "123" and self.password.get() == "123":
self.window.destroy()
Menu()
def login(self):
# Title Frame
title=Frame(self.window, border=2)
title.place(relx=0.5, rely=0.2, anchor=CENTER)
# Addng login form frame
frame=Frame(self.window,background="#CCD5CC",
highlightthickness=2 )
frame.place(relx=0.5, rely=0.45, anchor=CENTER)
# frame.pack()
# Company Label
company=Label(title, text=" RAJENDRA INVESTMENTS :: GUWAHATI",background="#CCD5CC",
font=("Calibri", 18, "bold"))
# self.company.insert(INSERT," RAJENDRA INVESTMENTS :: GUWAHATI")
# self.company.configure(font=("Calibri", 18, "bold"))
company.pack()
# Adding Heading Label
HeadLabel1=ttk.Label(frame,text="Login with your Credentials",background="#CCD5CC")
HeadLabel1.grid(row=0, column=0, pady=(10, 10), columnspan=2)
# username label
Username=ttk.Label(frame,text="Username: ",background="#CCD5CC")
Username.grid(row=1, column=0, pady=(10, 10), padx=(10,0))
# Adding a input field for username
self.username = StringVar()
e1 = ttk.Entry(frame, textvariable=self.username,background="#CCD5CC")
e1.grid(row=1, column=1,pady=(10, 10),padx=(0,10))
# password label
Password=ttk.Label(frame,text="Password: ",background="#CCD5CC")
Password.grid(row=2, column=0, pady=(10, 10), padx=(10,0))
# Adding a input field for password
self.password = StringVar()
e1 = ttk.Entry(frame, textvariable=self.password,background="#CCD5CC", show="*")
e1.grid(row=2, column=1,pady=(10, 10),padx=(0,10))
# Button
b1 = ttk.Button(frame, text="Login", width=9, command=self.log)
b1.grid(row=3, column=0,columnspan=2, pady=(0, 10))
def __init__(self):
# creating a window
self.window = Tk()
self.window.title("Finance Maester")
self.window.minsize(width=500, height=450)
self.window.maxsize(width=500, height=450)
self.window.resizable(0,0)
background_image=PhotoImage(file="images/123.gif")
background_label = Label( self.window, image=background_image)
background_label.pack()
self.login()
self.window.mainloop()
Login()
登录后进入菜单
from tkinter import *
from tkinter import ttk
from frontend.customerDetails import CustomerForm
class Menu:
def call_direct(self,key,value):
print(key, value)
if key=="New Customer" and value=="1":
self.window.destroy()
CustomerForm()
elif key=="Old Customer" and value=="3":
self.window.destroy()
elif key=="Current Customer" and value=="2":
self.window.destroy()
elif key=="Search" and value.strip(" ")!="":
self.window.destroy()
def menu(self):
# frames
frame1=Frame(self.window,background="#d5e1df")
frame1.place(relx=0.2, rely=0.25)
frame2=Frame(self.window,background="#d5e1df")
frame2.place(relx=0.2, rely=0.35)
# search label
search=ttk.Label(frame1, text="Search: ",background="#d5e1df")
search.grid(row=0, column=0,pady=(10, 10), padx=(0, 10))
# # Adding a input field for search
search = StringVar()
e1 = ttk.Entry(frame1, textvariable=search, background="#CCD5CC", width=27)
e1.grid(row=0, column=1, padx=(0, 10))
# Search Button
sb = ttk.Button(frame1, text="Go", width=15,command=lambda : self.call_direct("Search", search.get()))
sb.grid(row=0, column=2, padx=(0, 10),)
# Menu Button 1
mb1=ttk.Button(frame2, text=" New Customer ", command=lambda : self.call_direct("New Customer","1"))
mb1.grid(row=0, column=0, padx=(0, 10), pady=(10,0))
# Menu Button 2
mb2=ttk.Button(frame2, text=" Current Customer ", command=lambda : self.call_direct("Current Customer","2"))
mb2.grid(row=0, column=1, padx=(9, 10), pady=(10,0))
# Menu Button 3
mb3=ttk.Button(frame2, text=" Old Customer ", command=lambda : self.call_direct("Old Customer","3"))
mb3.grid(row=0, column=2, padx=(10, 10), pady=(10,0))
def __init__(self):
# creating a window
self.window = Tk()
self.window.title("Finance Maester")
self.window.minsize(width=500, height=450)
# self.window.maxsize(width=self.window.winfo_screenwidth(), height=self.window.winfo_screenheight())
# self.window.resizable(0,0)
self.window.configure(background="#d5e1df")
# self.window.attributes("-toolwindow"=> 1)
# self.background_image=PhotoImage(file="images/123.gif")
# self.background_label = Label( self.window, image=self.background_image)
# self.background_label.pack()
label=Label(self.window,text="RAJENDRA INVESTMENTS :: GUWAHATI",font=('Calibri',12,"bold"),background="#d5e1df")
# self.label.place(relx=0.05, rely=0.01)
label.pack(side="top", anchor="w", padx=(50, 0), pady=(10, 0))
self.menu()
self.window.mainloop()
新客户表单
from tkinter import *
from tkinter import ttk
from frontend.menu import Menu
from frontend.installmentDetails import InstallmentDetails
class CustomerForm():
def redirect(self, value):
self.window.destroy()
if value=="back":
Menu()
elif value=="continue":
InstallmentDetails()
def custForm(self):
# frames
frame1=Frame(self.window,background="#d5e1df")
frame1.pack(side="left", anchor="nw", padx=(50, 0), pady=(50, 0))
frame2=Frame(self.window,background="#d5e1df")
frame2.pack(side="left", anchor="ne",pady=(50, 0), padx=(0,50))
# frame1 blocks starts from here
# block 1 starts here
statement_file_no=ttk.Label(frame1,background="#d5e1df",text="STATEMENT FILE NUMBER : ", )
statement_file_no.grid(row=0, column=0, padx=(10, 10), pady=(10, 10), sticky="W" )
StFileNo=StringVar()
statement_file_no_entry=ttk.Entry(frame1, textvariable=StFileNo, width=25)
statement_file_no_entry.grid(row=0, column=1, padx=(10, 10), pady=(10, 10), sticky="W")
date=ttk.Label(frame1,background="#d5e1df", text="Date : ")
date.grid(row=0, column=2 , padx=(10, 10), pady=(10, 10), sticky="E")
datetext=StringVar()
date_entry=ttk.Entry(frame1, textvariable=datetext, width=22)
date_entry.grid(row=0, column=3, padx=(10, 10), pady=(10, 10), sticky="W")
hirer_name=ttk.Label(frame1,background="#d5e1df",text="HIRER NAME : ")
hirer_name.grid(row=1, column=0 , padx=(10, 10), pady=(10, 10), sticky="W")
hname=StringVar()
hirer_name_entry=ttk.Entry(frame1, textvariable=hname, width=73)
hirer_name_entry.grid(row=1, column=1, columnspan=3, padx=(10, 10), pady=(10, 10), sticky="W" )
Address=ttk.Label(frame1,background="#d5e1df", text="ADDRESS : ")
Address.grid(row=2, column=0, padx=(10, 10), pady=(10, 10), sticky="W")
address=StringVar()
Address_Text=Text(frame1, height=2, width=55)
Address_Text.grid(row=2, column=1, columnspan=3, padx=(10, 10), pady=(10, 10), sticky="W")
# block 1 ends here
# Back Button
sb = Button(frame2, text=" Back", width=20, font=("Calibri", 12),command=lambda: self.redirect("back"))
sb.grid(row=9, column=0, columnspan=3, padx=(10, 30), pady=(20, 15), rowspan=2)
# Submit Button
sb = Button(frame2, text=" Continue", width=20, font=("Calibri", 12),command=lambda: self.redirect("continue"))
sb.grid(row=9, column=1,columnspan=3, padx=(10, 10), pady=(20, 15), sticky="e")
def __init__(self):
# creating a window
self.window = Tk()
self.window.title("Finance Maester")
# self.window.minsize(width=500, height=450)
# self.window.maxsize(width=500, height=450)
# self.window.resizable(0, 0)
w, h = self.window.winfo_screenwidth(), self.window.winfo_screenheight()
self.window.geometry("%dx%d+0+0" % (w, h))
self.window.configure(background="#d5e1df")
# self.window.attributes("-toolwindow"=> 1)
# self.background_image=PhotoImage(file="images/123.gif")
# self.background_label = Label( self.window, image=self.background_image)
# self.background_label.pack(anchor="center")
self.label = Label(self.window, text="RAJENDRA INVESTMENTS :: GUWAHATI", font=('Calibri', 12, "bold"),
background="#d5e1df")
# self.label.place(relx=0.05, rely=0.01)
self.label.pack(side="top", anchor="w", padx=(50,0), pady=(10,0))
self.custForm()
self.window.mainloop()
在Customer类中,当我按下后退按钮时,我尝试调用Menu,我得到一个空窗口。我不知道为什么。