【问题标题】:Tkinter button does not appearTkinter 按钮不出现
【发布时间】:2021-03-22 20:49:56
【问题描述】:

我正在尝试为我的 D&D 角色构建一个小型 GUI,但我的第二个攻击按钮没有出现。

试图让它尽可能容易阅读。这是我第二次尝试编程,我发现 Tkinter 真的很难使用:(

用 Python 3 编写:

# Tkinter_buildframe #

root = tk.Tk()
frame = tk.Frame(root)
frame.pack( side = TOP )
frame.pack()
root.geometry("300x200")


# This is the charicter stats #
w = Label(root, text="""
charisma modifier = 4
spellcast attack_bonus = 7
""", font="12")
w.pack()


# Quit_button #
button = tk.Button(frame,
                   text="QUIT",
                   fg="red",
                   command=quit)
button.pack(side=tk.BOTTOM)

# Attack1 eldritch_blast_with_hex #

slogan = tk.Button(frame,
                   text="Eldritch Blast with Hex",
                   command=eldritch_blast_with_hex)
slogan.pack(side=tk.LEFT)


root.mainloop()
def popupmsg(msg):
    popup = tk.Tk()
    popup.wm_title("Eldritch Blast with Hex")
    label = ttk.Label(popup, text=msg, font=NORM_FONT)
    label.pack(side="top", fill="x", pady=10)
    B1 = ttk.Button(popup, text="Okay", command = popup.destroy)
    B1.pack()
    popup.mainloop()

# Attack2 eldritch_blast_without_hex#

slogan = tk.Button(frame,
                   text="Eldritch Blast without Hex",
                   command=eldritch_blast_without_hex)
slogan.pack(side=tk.LEFT)


root.mainloop()
def popupmsg(msg):
    popup = tk.Tk()
    popup.wm_title("Eldritch Blast without Hex")
    label = ttk.Label(popup, text=msg, font=NORM_FONT)
    label.pack(side="top", fill="x", pady=10)
    B1 = ttk.Button(popup, text="Okay", command = popup.destroy)
    B1.pack()

【问题讨论】:

  • 你最后有一些重复的代码,请务必证明正确的代码。还有哪个按钮没有出现在 GUI 中
  • 谢谢!我删除了以下内容,只保留了(2个)然后它起作用了。 root.mainloop() def popupmsg(msg): popup = tk.Tk() popup.wm_title("Eldritch Blast without Hex") label = ttk.Label(popup, text=msg, font=NORM_FONT) label.pack(side ="top", fill="x", pady=10) B1 = ttk.Button(popup, text="Okay", command = popup.destroy) B1.pack()
  • 最后的 Root.mainloop() 在代码中被复制了。删除并保留一个。然后就成功了!
  • @CoolCloud 完成! ;)

标签: python python-3.x tkinter button position


【解决方案1】:

代码中有多个root.mainloop(),它只是暂停代码继续执行其余代码。所以删除root.mainloop() 之一并将其放在代码末尾。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多