【问题标题】:ive been working on this mini project in tkinter我一直在 tkinter 做这个迷你项目
【发布时间】:2021-12-11 10:05:38
【问题描述】:

我一直在使用 Tkinter 做这个项目,我做了一个 if 语句,说如果用户输入的答案是 cs 打印出标签说正确,否则打印错误,我已经测试过了,但它一直说当我输入“cs”时,它的答案是错误的

import Tkinter as tk

window = tk.Tk()

window.geometry("800x600")
window.resizable(width=False, height=False)

label1 = tk.Label(window, text='Quiz App', font=("Arial Bold", 25))
label1.pack()


def playbuttonclicked():
    label1.destroy()
    playbtn.destroy()
    quitbtn.destroy()
    label2 = tk.Label(window, text='What is the short form of computer science', font=("Arial Bold", 10)).pack()
    txtbox = tk.Entry(window, width=50)
    txtbox.place(x=250, y=400)
    useranswer = txtbox.get()

    def chkanswer():
        if useranswer == 'cs':
            lbl = tk.Label(window, text='Correct').pack()
        else:
            lbl = tk.Label(window, text='Wroooong').pack()
    submitbtn = tk.Button(window, text='Submit', command=chkanswer).pack(side=tk.BOTTOM)



playbtn = tk.Button(window, text='Play', font=("Arial Bold", 10), command=playbuttonclicked)
playbtn.pack(side=tk.BOTTOM)


def quitbuttonclicked():
    window.destroy()


quitbtn = tk.Button(window, text='Quit', font=("Arial Bold", 10), command=quitbuttonclicked)
quitbtn.place(x=530, y=570)

window.mainloop()

你能帮帮我吗

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: python if-statement tkinter


【解决方案1】:

您在创建条目后直接阅读条目(文本框)中的内容。它不会等到您输入文本。

此外,您在 playbuttonclicked() 函数内创建条目,并且在函数结束后引用 txtbox 将不可用。

在我的示例中,我在全局命名空间中创建条目,playbuttonclicked() 函数仅将其放置在窗口上。

当用户点击Submit按钮时,chkanswer()函数将从Entry中获取字符串,然后将其与正确答案进行比较。

import tkinter as tk

window = tk.Tk()

window.geometry("800x600")
window.resizable(width=False, height=False)

label1 = tk.Label(window, text='Quiz App', font=("Arial Bold", 25))
label1.pack()

txtbox = tk.Entry(window, width=50) # Create Entry in global namespace

def chkanswer():
    useranswer = txtbox.get()       # Get contents from Entry
    if useranswer == 'cs':
        lbl = tk.Label(window, text='Correct').pack()
    else:
        lbl = tk.Label(window, text='Wroooong').pack()

def playbuttonclicked():
    label1.destroy()
    playbtn.destroy()
    quitbtn.destroy()
    label2 = tk.Label(window, text='What is the short form of computer science', font=("Arial Bold", 10)).pack()
    txtbox.place(x=250, y=400) 
    useranswer = txtbox.get()
    submitbtn = tk.Button(window, text='Submit', command=chkanswer).pack(side=tk.BOTTOM)

playbtn = tk.Button(window, text='Play', font=("Arial Bold", 10), command=playbuttonclicked)
playbtn.pack(side=tk.BOTTOM)

def quitbuttonclicked():
    window.destroy()

quitbtn = tk.Button(window, text='Quit', font=("Arial Bold", 10), command=quitbuttonclicked)
quitbtn.place(x=530, y=570)

window.mainloop()

【讨论】:

  • 如果你的答案是唯一一个直到凌晨 5 点才可用的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多