我对 Python 相当陌生,并且在使用 Tkinter 文本框时遇到错误。我无法让一个出现在我的窗口中。由于这个原因,我的程序现在无法运行。
我出现一个按钮,但没有出现文本框。这是我正在从事的项目的一个非常简单的密码猜测器。你输入一个数字,它会尝试猜测你输入的数字。我测试了很多不同的在线资源并从 Google 复制了代码,我要么得到相同的结果,要么我的窗口根本没有出现。
如果您只想在 tkinter 中获取一些用户输入并将其与生成的变量进行比较,这里有一个方法,我希望这可以帮助您:
from tkinter import Tk, Button, Entry, Label
import random
root = Tk()
e1 = Entry(root)
e1.pack()
def click():
user_input = e1.get()
# Generate your random password (using a number for simplicity)
a = random.randrange(1, 10)
if int(user_input) == a:
l1 = Label(root, text="You guessed the password")
l1.pack()
b1 = Button(root, text="Check password", command=click)
b1.pack()
root.mainloop()
如果您还有任何疑问,请告诉我。