【问题标题】:Why Won't My Button Be Displayed??为什么我的按钮不显示??
【发布时间】:2014-03-16 09:33:42
【问题描述】:
#Last one Looses (II)
from tkinter import *
from tkinter import ttk
root = Tk()

#window
root.title("Last one Looses Mark II")

#Counters Entry
counters = StringVar()
countersL = Label(root, text = "How many counters do you want to play with (10-50)")
countersL.pack(side = LEFT)
countersE = Entry(root, textvariable = counters, bd = 5)
countersE.pack(side = RIGHT)
#Function to process this
def countersinput():
    no_counters = int(input(counters.get()))
no_counters = int(input(counters.get()))
#Submit Button
countersB = Button(root, text = "Submit", command = countersinput)
countersB.pack(side = BOTTOM)
#Making sure the counters are between 10-50
while no_counters > 50 or no_counters < 10:
    Error = Message(root, text="You need to pick between 10 and 50 counters...")
    Error.pack()
    counters = StringVar()
    countersL = Label(root, text = "How many counters do you want to play with (10-50)")
    countersL.pack(side = LEFT)
    countersE = Entry(root, textvariable = counters, bd = 5)
    countersE.pack(side = RIGHT)
    def countersinput():
        no_counters = int(input(counters.get()))
    countersB = Button(root, text = "Submit", command = countersinput)
    countersB.pack(side = BOTTOM)

#Sucess Message
Sucess = Message(root, text=("You are playing with",no_counters,"counters"))

root.mainloop()

每当我在 IDLE 中运行它时,tkinter 窗口都不会出现,当我将它运行到命令行(python one...)时,它会显示窗口但没有“提交”按钮。

我真的很困惑,请帮忙!

【问题讨论】:

    标签: python button tkinter


    【解决方案1】:

    首先,您是否意识到您的 GUI 程序要求从命令行输入,并且您在创建提交按钮之前 这样做了?这就是提交按钮未显示的原因。如果您正在编写 GUI 程序,则不应尝试从命令行读取输入。

    如果您从终端输入 10 到 50 之间的数字,您的代码应该会显示一个窗口。但是,如果您输入超出该范围的数字,则由于while no_counters ... 循环,将不会显示任何内容。该循环将无限运行,阻止与 GUI 的任何其他交互,并阻止任何其他小部件出现。

    【讨论】:

    • 对,我有点困惑,你能澄清一下你的意思吗
    • @user3262355:澄清什么部分。你不明白我所说的“从命令行请求输入”是什么意思吗?或者你不明白为什么while循环是无限的?你明白什么是事件循环为什么是必要的吗?
    • 好吧,我应该放什么而不是 while 循环...以及我如何要求输入并在程序的其余部分中使用答案...全部在 tkinter :)
    猜你喜欢
    • 2012-02-27
    • 2017-06-06
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    相关资源
    最近更新 更多