我正在尝试创建一个文本小部件,以便用户可以输入数据。这是我的第一个GUI,所以任何帮助将不胜感激。这是我写的程序中的第四课。每当我尝试这个时,我都会收到一条错误,上面写着“名称错误:未定义userentry”。谢谢!
import tkinter as tk
from tkinter import ttk
LARGE_FONT= ("Verdana", 16)
class ConejoApp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title("Fur Their Health")
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 (StartPage, PageOne, PageTwo, PageThree, PageFour):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="Pet Rabbit Health", fg="blue", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button = ttk.Button(self, text="General Care",
command=lambda: controller.show_frame(PageOne))
button.pack()
button2 = ttk.Button(self, text="Produce List",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
button3 = ttk.Button(self, text="Weight Check",
command=lambda: controller.show_frame(PageThree))
button3.pack()
button4 = ttk.Button(self, text="Rabbit-to-Human Years Calculator",
command=lambda: controller.show_frame(PageFour))
button4.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="General Care", fg="blue", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = ttk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Produce List", fg="blue", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button1 = ttk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
class PageThree(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Is your rabbit at a healthy weight?", fg="blue", font=LARGE_FONT)
label.pack(pady=10,padx=10)
button2 = ttk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button2.pack()
class PageFour(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Rabbit to Human Years Calculator", fg="blue", font=LARGE_FONT)
label.pack(pady=10,padx=10)
label = tk.Label(self, text="Age:", fg="blue", font=LARGE_FONT)
label.pack()
userentry = tk.Entry(self)
userentry.pack()
btnCalc=ttk.Button(self, text="Calculate")
btnCalc.pack()
btnCalc["command"]=self.calculateAge
label2=tk.Label(self, text= "Here's your rabbit's human age:", fg="blue", font=LARGE_FONT)
label2.pack()
button1 = ttk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
def calculateAge(self):
rabbitage= int(userentry.get())
age=(rabbitage*8)
label2["text"]=str(age)
app = ConejoApp()
app.mainloop()
答案 0 :(得分:1)
您的问题在于导入样式。如果您将tkinter导入为tk
,则您的输入必须为e = tk.Entry()
。如果您希望它只是e = Entry()
导入tkinter,就像这样from tkinter import *
。
修改
此外,您的脚本中也出现了错误。你需要的时候不要打电话给self
而你不需要的时候打电话给你!
以下是工作代码:
class PageFour(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Rabbit to Human Years Calculator", fg="blue", font=LARGE_FONT)
label.pack(pady=10,padx=10)
label = tk.Label(self, text="Age:", fg="blue", font=LARGE_FONT)
label.pack()
self.userentry = tk.Entry(self)
self.userentry.pack()
btnCalc=ttk.Button(self, text="Calculate")
btnCalc.pack()
btnCalc["command"]=self.calculateAge
self.label2=tk.Label(self, text= "Here's your rabbit's human age:", fg="blue", font=LARGE_FONT)
self.label2.pack()
button1 = ttk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
def calculateAge(self):
rabbitage= int(self.userentry.get())
age=(rabbitage*8)
self.label2["text"]=str(age)