【问题标题】:How to Set a Buttons Width in Tkinter (Python 3) [duplicate]如何在 Tkinter(Python 3)中设置按钮宽度 [重复]
【发布时间】:2016-02-25 09:40:28
【问题描述】:

我想在 python 3 中制作一个简单的测验程序,但我不知道如何让按钮达到我想要的全宽。我正在使用 python 3 和 TKinter 模块来制作窗口和所有按钮。

from tkinter import *

root = Tk()

que_labl = Label(root, text='Question')
choice1 = Button(root, text='Choice one')
choice2 = Button(root, text='Choice one plus one')
choice3 = Button(root, text='Choice 3')
choice4 = Button(root, text='Choice eight divided by 2')

que_labl.grid(row=0, columnspan=2)
choice1.grid(row=2, column=0, sticky=W)
choice2.grid(row=2, column=1, sticky=W)
choice3.grid(row=3, column=0, sticky=W)
choice4.grid(row=3, column=1, sticky=W)

root.mainloop()

代码生成一个像这样的窗口:

【问题讨论】:

  • 简单的谷歌搜索会给你正确的答案
  • 问问自己“sticky=W 是做什么的?”

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


【解决方案1】:

使用Grid.rowconfigure()Grid.columnconfigure()sticky=E+W

from Tkinter import *

root = Tk()
#Configure line 0 and 1
Grid.rowconfigure(root, 0, weight=1)
Grid.rowconfigure(root, 1, weight=1)

#Configure column 0 and 1
Grid.columnconfigure(root, 0, weight=1)
Grid.columnconfigure(root, 1, weight=1)

que_labl = Label(root, text='Question')
choice1 = Button(root, text='Choice one')
choice2 = Button(root, text='Choice one plus one')
choice3 = Button(root, text='Choice 3')
choice4 = Button(root, text='Choice eight divided by 2')

que_labl.grid(row=0, columnspan=2)
choice1.grid(row=2, column=0, sticky=E+W)
choice2.grid(row=2, column=1, sticky=E+W)
choice3.grid(row=3, column=0, sticky=E+W)
choice4.grid(row=3, column=1, sticky=E+W)

root.mainloop()

【讨论】:

  • 为了迂腐,对于这个特定的问题,粘性值不需要包含 N 和 S。
  • @BryanOakley 谢谢。这是一个表格解决方案。
猜你喜欢
  • 2011-05-20
  • 1970-01-01
  • 2017-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-14
  • 1970-01-01
相关资源
最近更新 更多