【问题标题】:Button Not Appearing in Tkinter按钮未出现在 Tkinter 中
【发布时间】:2021-10-20 18:41:26
【问题描述】:

我的Button 没有出现在 tkinker (Python) 中。
这是主要代码:

# Start Of Manual Scanner ------------------------------------
manual = LabelFrame(tab_1,
                    text="Manual")
manual.pack(side=LEFT)
main = Label(manual,
             text="Manual Entry",
             font=('Arial', 20))
main.pack(side=TOP)
manual_entry = Entry(manual,
                     width=100,
                     font=('Arial', 20))
manual_entry.pack(side=RIGHT)
place_order_BTN = Button(manual,
                         text="Add Item",
                         fg='black',
                         font=('Arial', 20),
                         width=20,
                         bg='#feffa3',
                         activeforeground='white',
                         activebackground='black',)
place_order_BTN.pack(pady=15)

当我运行它时:

【问题讨论】:

  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。
  • 我无法重现将tab_1 替换为根Tk() 窗口的问题。看起来您可能正在使用ttk.Notebook,这可能与问题有关。请提供minimal reproducible example,因为您问题中当前代码的 sn-p 中没有足够的信息。
  • 条目manual_entry 太长(100 个字符宽度),因此按钮被压缩为零宽度。尝试将manual_entry.pack(side=RIGHT) 更改为manual_entry.pack()

标签: python tkinter tkinter-button


【解决方案1】:

该按钮实际上被打包在条目的左侧。但是,由于该条目有点长(100 个字符宽度),它会将按钮压缩为零宽度。

您可以将条目缩小一点:

manual_entry = Entry(manual,
                     width=30,  # changed from 100 to 30
                     font=('Arial', 20))
manual_entry.pack(side=RIGHT)

或将manual_entry.pack(side=RIGHT) 更改为manual_entry.pack()

【讨论】:

  • 如果Button 也被明确地赋予width 会发生什么?
  • @martineau OP 已经指定了按钮的宽度选项。
  • 糟糕……你是对的。我只是想知道为什么Button 没有缩小Entry 而不是相反。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-20
  • 2012-04-20
  • 1970-01-01
相关资源
最近更新 更多