我对Python很新,但我正在努力学习。我正在使用树莓派,而我正在使用Python 3.2.3版。我的问题是我的框架中有一个文本小部件,可以让用户添加文本。我在下面有一个名为“运行代码”的按钮,当点击它时,它会调用一个函数来检查文本框中的输入。如果字符是按字母顺序排列的,那么它会在标签上打印一条欢迎信息,我的分数应该增加5分,如果输入不是按字母顺序排列则显示错误信息。我试过使用if语句,但只有else部分正在执行。有什么建议?我的代码如下:
import tkinter as tk
from tkinter import ttk
score = 0
class Game(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Game")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
menubar = tk.Menu(container)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="Exit", command=quit)
menubar.add_cascade(label="File", menu=filemenu)
tk.Tk.config(self, menu=menubar)
self.frames = {}
for F in (StartPage, Py2Page1):
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)
self.columnconfigure(0,weight=1)
self.columnconfigure(1,weight=1)
label = tk.Label(self, text="Title")
label.grid(row=0,column=0, columnspan=2)
label1 = tk.Label(self, text ="Add text here")
label1.grid(row=1,column=0, columnspan=2, pady=10)
button1 = ttk.Button(self, text="Start", command=lambda: controller.show_frame(Py2Page1))
button1.grid(row=2,column=0, columnspan=2,pady=10)
class Py2Page1(tk.Frame):
def __init__(self, parent,controller):
tk.Frame.__init__(self, parent)
result = tk.StringVar()
self.columnconfigure(0,weight=1,minsize=640)
self.columnconfigure(1,weight=1,minsize=640)
label = tk.Label(self, text="Title!")
label.grid(row=0,column=0,columnspan=2)
self.status = tk.Label(self)
self.status.grid(row=0,column=1,sticky="e",padx=30)
label2 = tk.Label(self, text ="""SubTitle""")
label2.grid(row=1,column=0)
label3 = tk.Label(self, text ="""
Add text here....
""", justify="left")
label3.grid(row=2,column=0,sticky="n")
self.user_input = tk.Text(self,wrap="word")
self.user_input.grid(column=1,row=2)
button4 = ttk.Button(self,text="Run Code", command = self.checking)
button4.grid(column=1,row=3)
button5 = ttk.Button(self,text="Clean", command = self.clear)
button5.grid(column=1,row=4)
self.output = tk.Label(self)
self.output.grid(column=1,row=5,sticky='w', pady=10,padx=10)
def checking(self):
answer = self.user_input.get('1.0',tk.END)
if answer.isalpha():
result = "Hello " + answer
scorechanged= score =+ 5
else:
result = "Please enter your name again"
self.output.config(text=result)
self.status.config(text=scorechanged)
def clear(self):
self.user_input.delete('1.0',tk.END)
app = Game()
app.geometry("1280x720")
app.mainloop()
由于
答案 0 :(得分:0)
解决最初的问题:
userInput = stringVar()
userEntry = Entry(master, variable=userInput) #creates an entry box
userInput.get() # returns what is in the entry box
我可以想象用这段代码你可以完成你想要的目标。您可以使用if语句查看输入框中的内容并确定天气是一个数字。
try:
int(userInput.get()) # try and convert to integer
except ValueError: # if the conversion fails due to it containing letters
print('userInput is not a number')