Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
User interface 简单的图形用户界面的数字猜测游戏_User Interface_Random_Tkinter_Python 3.4 - Fatal编程技术网

User interface 简单的图形用户界面的数字猜测游戏

User interface 简单的图形用户界面的数字猜测游戏,user-interface,random,tkinter,python-3.4,User Interface,Random,Tkinter,Python 3.4,好的,我删除了while循环并添加了文本字段,这样答案将有一个主页 现在它可以工作了,但它总是说“太低”,不允许多个条目 我错过了什么?我相信这很接近 from tkinter import * import random class Application(Frame): """ GUI Application for Number Guessing Game. """ def __init__(self, master): super(Application,

好的,我删除了while循环并添加了文本字段,这样答案将有一个主页

现在它可以工作了,但它总是说“太低”,不允许多个条目

我错过了什么?我相信这很接近

from tkinter import *
import random
class Application(Frame):
    """ GUI Application for Number Guessing Game. """
    def __init__(self, master):
        super(Application, self).__init__(master)
        self.grid()
        self.create_widgets()
    # create widgets
    def create_widgets(self):
        Label(self,
              text = "Pick a number, 1 - 100: "
              ).grid(row = 0, column = 0, sticky = W)
        self.number_ent = Entry(self)
        self.number_ent.grid(row = 1, column = 1, sticky = W)
        Button(self,
               text = "Submit",
               command = self.get_answer
               ).grid(row = 2, column = 1, sticky = W)
        self.answer_txt = Text(self, width = 40, height = 10, wrap = WORD)
        self.answer_txt.grid(row = 3, column = 0, columnspan = 4)
    # get answer
    def get_answer(self):
        secret_num = random.randint(1, 100)
        number_ent = 0
        if number_ent < secret_num:
            answer = "Too low, guess again."
        elif number_ent > secret_num:
            answer = "Too high, guess again."
        elif number_ent == secret_num:
            answer = "Congratulations, you guessed right!"
        self.answer_txt.delete(0.0, END)
        self.answer_txt.insert(0.0, answer)
root = Tk()
root.title("Number Guess")
app = Application(root)
root.mainloop()
从tkinter导入*
随机输入
课程申请(框架):
“”“用于猜数字游戏的GUI应用程序。”“”
定义初始(自我,主):
超级(应用程序,自我).\uuuu初始化\uuuuuu(主)
self.grid()
self.create_widgets()
#创建小部件
def创建_小部件(自):
标签(自我,
text=“选择一个数字,1-100:”
).grid(行=0,列=0,粘性=W)
self.number\u ent=条目(self)
自编号网格(行=1,列=1,粘性=W)
按钮(自我,
text=“提交”,
command=self.get\u答案
).grid(行=2,列=1,粘性=W)
self.answer_txt=Text(self,宽度=40,高度=10,换行=WORD)
self.answer_txt.grid(行=3,列=0,列span=4)
#得到答案
def获取_答案(自我):
secret_num=random.randint(1100)
数字=0
如果编号机密编号:
回答=“太高了,再猜一次。”
elif编号=机密编号:
回答=“祝贺你,你猜对了!”
self.answer_txt.delete(0.0,结束)
self.answer_txt.insert(0.0,答案)
root=Tk()
root.title(“数字猜测”)
app=应用程序(根)
root.mainloop()

在GUI代码中不能有这样的while循环。GUI仅在允许运行主循环时工作,并且只要您有自己的内部循环,
mainloop
就无法运行


GUI编程不同于传统的自顶向下的过程代码。使用GUI编程,您必须设置对事件作出反应的函数和变量。然后用户按submit,您需要检查一两件事情,然后返回。根据经验,为响应按钮单击或事件而调用的任何函数运行时间不应超过几百毫秒。

这样更好。重新开放。