使用 if 标签在 tlinker 中不起作用

发布于 2025-01-15 14:00:24 字数 604 浏览 1 评论 0原文

我正在尝试使用 tkinter 进行实验,但我的代码无法正常工作。谁能告诉我我做错了什么?

import tkinter as tk

window = tk.Tk()

frame = tk.Frame(master=window, width=150, height=150)
frame.pack()

label1 = tk.Label(master=frame, text="I'm at (0, 0)", bg="red")
label1.place(x=0, y=0)

label2 = tk.Label(master=frame, text="I'm at (75, 75)", bg="yellow")
Question = input("your question")
if Question == ("yes"):
    print ("well done")
elif Question == ("no"):
    print ("try again")
    label3 = tk.Label(master=frame, text="I'm at (75, 75)", bg="yellow")
    label3.place(x=75, y=75)
    
    
window.mainloop()

I am trying to do experiments with tkinter, but my code isn't working. Can anyone tell me what am I doing wrong?

import tkinter as tk

window = tk.Tk()

frame = tk.Frame(master=window, width=150, height=150)
frame.pack()

label1 = tk.Label(master=frame, text="I'm at (0, 0)", bg="red")
label1.place(x=0, y=0)

label2 = tk.Label(master=frame, text="I'm at (75, 75)", bg="yellow")
Question = input("your question")
if Question == ("yes"):
    print ("well done")
elif Question == ("no"):
    print ("try again")
    label3 = tk.Label(master=frame, text="I'm at (75, 75)", bg="yellow")
    label3.place(x=75, y=75)
    
    
window.mainloop()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆那么伤 2025-01-22 14:00:24

在制作 GUI 时,input 可以替换为 tk.Entry 输入仅用于控制台。我向 tk.LabeltkEntryen.bind 添加了一项功能和其他小部件。我替换了你的代码并将重新写入我的代码中。

这是代码:

import tkinter as tk


window = tk.Tk()
window.geometry('500x200')

frame = tk.Frame(master=window, width=190, height=150)
frame.pack()

lbl = tk.Label(master=frame, text="Answer yes or no in entry box", bg='black', fg="white")
lbl.place(x=5, y=0)

label1 = tk.Label(master=frame, text="I'm at (0, 0)", bg="red")
 
en = tk.Entry(frame, width=25)
en.place(x=125, y=30)
out = tk.StringVar()

def _get(event):
    global p
    p = en.get()
    if p == ("yes"):
        label1.place(x=50, y=30)
        out = "well done"       
    elif p == ("no"):
        out = "try again" 
        label2.place(x=75, y=50)
    label3.configure(text=out)    
                        
en.bind('<Return>' , _get)

label2 = tk.Label(master=frame, text="I'm at (75, 75)", bg="yellow")

label3 = tk.Label(master=frame, text="",  bg="yellow")
label3.place(x=75, y=80)
   
window.mainloop()

只需按 Enter 键即可。

是的结果:

在此处输入图像描述

否的结果:

在此处输入图像描述

The input can be substituted to tk.Entry while making GUI The input is merely used for console. I added one more feature and additional widgets to tk.Label, tkEntry and en.bind. I substituted in your code and will re-wrote into my code.

Here is code:

import tkinter as tk


window = tk.Tk()
window.geometry('500x200')

frame = tk.Frame(master=window, width=190, height=150)
frame.pack()

lbl = tk.Label(master=frame, text="Answer yes or no in entry box", bg='black', fg="white")
lbl.place(x=5, y=0)

label1 = tk.Label(master=frame, text="I'm at (0, 0)", bg="red")
 
en = tk.Entry(frame, width=25)
en.place(x=125, y=30)
out = tk.StringVar()

def _get(event):
    global p
    p = en.get()
    if p == ("yes"):
        label1.place(x=50, y=30)
        out = "well done"       
    elif p == ("no"):
        out = "try again" 
        label2.place(x=75, y=50)
    label3.configure(text=out)    
                        
en.bind('<Return>' , _get)

label2 = tk.Label(master=frame, text="I'm at (75, 75)", bg="yellow")

label3 = tk.Label(master=frame, text="",  bg="yellow")
label3.place(x=75, y=80)
   
window.mainloop()

Just press enter.

Result for yes:

enter image description here

Result for no:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文