所以我正在编写一个程序,通过冒泡来排序婴儿名字。该程序要求2个用户在打开的选项卡上输入10的评级。但是,当用户按下我输入的输入按钮时,我无法找到更新窗口的任何方法。
from tkinter import *
BoyList = [['a',0],['b',0],['c',0],['d',0],['e',0],['f',0],['h',0],['i',0],['j',0]]
GirlList = [['a',0],['b',0],['c',0],['d',0],['e',0],['f',0],['h',0],['i',0],['j',0]]
目前这些名单中都有占位符
Number1 = 0
Number2 = 0
Count = 0
def MainMenu():
Menu = Tk()
Menu.title('MainMenu')
MenuLab = Label(Menu, text = 'Main Menu')
MenuLab.pack()
BoyButton = Button(Menu, text = 'Select a boy name', command = lambda Menu=Menu:NameRuleBoy(Menu))
BoyButton.pack()
GirlButton = Button(Menu, text = 'Select a girl name', command = lambda Menu=Menu:NameRuleGirl(Menu))
GirlButton.pack()
QuitButton = Button(Menu, text = 'Quit', command = lambda Menu=Menu:QuitButton(Menu))
QuitButton.pack()
Menu.mainloop()
def QuitButton(Menu):
Menu.destroy()
def GirlBackButt(RuleGirlName):
RuleGirlName.destroy()
MainMenu()
def NameRuleGirl(Menu):
Menu.destroy()
RuleGirlName = Tk()
RuleGirlName.title('Naming rules')
RulLab = Label(RuleGirlName, text = 'When a name comes up rate the name out of ten (ten being like it a lot)')
RulLab.pack()
ContinueButt = Button(RuleGirlName, text = 'Continue', command = lambda RuleGirlName=RuleGirlName:NameGirl(RuleGirlName))
ContinueButt.pack()
BackButton = Button(RuleGirlName, text = 'Back', command = lambda RuleGirlName=RuleGirlName:GirlBackButt(RuleGirlName))
BackButton.pack()
RuleGirlName.mainloop()
def NameGirl(RuleGirlName):
RuleGirlName.destroy()
GirlName = Tk()
NameLabel = Label(GirlName, text = GirlList[0][0])
NameLabel.pack()
ParentLabel1 = Label(GirlName, text = 'Parent 1 put score here:')
ParentLabel1.pack()
Entry1 = Entry(GirlName)
Entry1.pack()
ParentLabel2 = Label(GirlName, text = 'Parent 2 put score here:')
ParentLabel2.pack()
Entry2 = Entry(GirlName)
Entry2.pack()
EnterButton = Button(GirlName, text = 'Enter', command = lambda GirlNamme=GirlName:GetButt(Entry1,Entry2,GirlName,Count))
EnterButton.pack()
GirlName.mainloop()
def GetButt(Entry1, Entry2,GirlName,Count):
Number1 = Entry1.get()
Number2 = Entry2.get()
Number1 = int(Number1)
Number2 = int(Number2)
print(Number1,Number2)
WindowUpdate(Count)
return (Number1,Number2)
def WindowUpdate(Count):
Count += 1
GirlList[Count][0]
MainMenu()
如果需要帮助我处理代码,我已经包含了所有Tkinter窗口的代码。另一方面:你能不能帮助我知道为什么我在函数之外测试时我的函数中的所有计算都返回0?
答案 0 :(得分:0)
我试图在不使用类的情况下使一切正常工作,因为你没有在你的例子中使用过类。然而,代码变得有点凌乱,global
到处都是......
我们需要能够跟踪GirlList
中检查的最后一个索引,并在按下提交按钮后更新Toplevel()
窗口。这最好用一个类实现,我们可以避免同时使用全局。
在我的下面的例子中,根窗口将打开一个顶层窗口并显示第一个选项。按下提交按钮后,它将处理平均值并更新列表。然后它将清除输入字段并更新到下一个列表项。
以下是我使用类示例的答案:
import tkinter as tk
from tkinter import messagebox
class MyApp(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.master = master
self.GirlList = [['a',0],['b',0],['c',0],['d',0],['e',0],['f',0],['h',0],['i',0],['j',0]]
self.master = master
self.master.title('MainMenu')
self.gname_x = 0
MenuLab = tk.Label(self.master, text='Main Menu')
MenuLab.pack()
GirlButton = tk.Button(self.master, text='Select a girl name', command=self.NameRuleGirl)
GirlButton.pack()
QuitButton = tk.Button(self.master, text='Quit', command=self.QuitButton)
QuitButton.pack()
def QuitButton(self):
self.master.destroy()
def NameGirl(self):
if self.gname_x <= len(self.GirlList):
self.top = tk.Toplevel(self.master)
self.name_label = tk.Label(self.top, text=self.GirlList[self.gname_x][0])
self.name_label.pack()
tk.Label(self.top, text='Parent 1 put score here:').pack()
self.Entry1 = tk.Entry(self.top)
self.Entry1.pack()
tk.Label(self.top, text='Parent 2 put score here:').pack()
self.Entry2 = tk.Entry(self.top)
self.Entry2.pack()
tk.Button(self.top, text='Enter', command=self.update_girl_name).pack()
else:
messagebox.showinfo("Error", "You have already reviewed the girl names!")
def update_girl_name(self):
valid_list = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
if self.Entry1.get().strip() in valid_list and self.Entry2.get().strip() in valid_list:
average_vote = (int(self.Entry1.get().strip()) + int(self.Entry2.get().strip())) / 2
self.GirlList[self.gname_x][1] = average_vote
self.gname_x += 1
if self.gname_x < len(self.GirlList):
self.name_label.config(text=self.GirlList[self.gname_x][0])
self.Entry1.delete(0, "end")
self.Entry2.delete(0, "end")
else:
self.top.destroy()
print(self.GirlList)
else:
messagebox.showinfo("Error", "Only numbers 1 through 10 are valid!")
self.top.tkraise()
self.Entry1.delete(0, "end")
self.Entry2.delete(0, "end")
def NameRuleGirl(self):
answer = messagebox.askquestion("Naming rules", "When a name comes up rate the name out of ten.\n(ten being like it a lot and 1 being not at all).\n\n Do you wish to continue?")
if answer == "yes":
self.NameGirl()
if __name__ == "__main__":
root=tk.Tk()
MyApp(root).pack()
root.mainloop()