老实说,我不记得我为使代码正常工作所做的所有更改,我记得的主要更改是删除所有未使用的模块和代码,并使其遵循
PEP 8 - Style Guide for Python Code
更接近(不完全)。
另一个主要修改是
不
导出您的
initialize_screen
(已重命名
InitializeScreen
)分类自
tk.TK
,并让它改为呼叫
tk.Toplevel()
创建与主窗口分离的第二个窗口。与此相关,我将呼叫转移到
tk.Tk()
进入主脚本。
以下是两个脚本的版本,我认为这两个脚本共同展示了如何完成您所说的尝试:
_推荐食物。py公司
import Tkinter as tk
class InitializeScreen(object):
def __init__(self):
self.win = tk.Toplevel(takefocus=True) # Create a new window.
def draw_screen(self):
## User_Name
self.User_Name_Label = tk.Label(self.win, text="User Name :")
self.User_Name_Label.grid(column=0, row=0)
self.User_Name = tk.StringVar()
self.Name_Input = tk.Entry(self.win, width=12, textvariable=self.User_Name)
self.Name_Input.grid(column=1, row=0)
## User_id
self.User_id_Label = tk.Label(self.win, text="User id :")
self.User_id_Label.grid(column=0, row=1)
self.id = tk.StringVar()
self.User_id_Input = tk.Entry(self.win, width=12, textvariable=self.id)
self.User_id_Input.grid(column=1, row=1)
## Food1
self.Food1_Label = tk.Label(self.win, text="Food1 :")
self.Food1_Label.grid(column=0, row=2)
self.Food1 = tk.StringVar()
self.Food1_Input = tk.Entry(self.win, width=12, textvariable=self.Food1)
self.Food1_Input.grid(column=1, row=2)
## Food1_rating
self.Food1_rating_Label = tk.Label(self.win, text="Rating :")
self.Food1_rating_Label.grid(column=2, row=2)
self.Food1_rating = tk.StringVar()
self.Food1_rating_Input = tk.Entry(self.win, width=12, textvariable=self.Food1_rating)
self.Food1_rating_Input.grid(column=3, row=2)
## Food2
self.Food2_Label = tk.Label(self.win, text="Food2 :")
self.Food2_Label.grid(column=0, row=3)
Food2 = tk.StringVar()
self.Food2_Input = tk.Entry(self.win, width=12, textvariable=Food2)
self.Food2_Input.grid(column=1, row=3)
## Food2_rating
self.Food2_rating_Label = tk.Label(self.win, text="Rating :")
self.Food2_rating_Label.grid(column=2, row=3)
self.Food2_rating = tk.StringVar()
self.Food2_rating_Input = tk.Entry(self.win, width=12, textvariable=self.Food2_rating)
self.Food2_rating_Input.grid(column=3, row=3)
## Food3
self.Food3_Label = tk.Label(self.win, text="Food3 :")
self.Food3_Label.grid(column=0, row=4)
self.Food3 = tk.StringVar()
self.Food3_Input = tk.Entry(self.win, width=12, textvariable=self.Food3)
self.Food3_Input.grid(column=1, row=4)
## Food3_rating
self.Food3_rating_Label = tk.Label(self.win, text="Rating :")
self.Food3_rating_Label.grid(column=2, row=4)
self.Food3_rating = tk.StringVar()
self.Food3_rating_Input = tk.Entry(self.win, width=12, textvariable=self.Food3_rating)
self.Food3_rating_Input.grid(column=3, row=4)
## Submit button
self.submit = tk.Button(self.win, text="Submit", command=self.print_val)
self.submit.grid(column=0,row=5)
## Reset button
self.reset = tk.Button(self.win, text="Reset")
self.reset.grid(column=1, row=5)
def print_val(self):
name1 = self.User_Name.get()
print('name1: {!r}'.format(name1))
_主屏幕。py公司
import Tkinter as tk
from _Recommendation_by_food import InitializeScreen
def call_screen_1():
fd = InitializeScreen()
fd.draw_screen()
win = tk.Tk()
win.title('Recommender System For Foods')
win.geometry('500x500')
B1 = tk.Button(win, text="Recommendation By Food", width=30, command=call_screen_1)
B1.pack()
win.mainloop()