【问题标题】:How to set a specified positions for RadioBox and Slider steps with tkinter如何使用 tkinter 为 RadioBox 和 Slider 步骤设置指定位置
【发布时间】:2019-10-29 21:43:06
【问题描述】:

我是 tkinter 的新手。 我正在尝试制作 GUI,目前我制作了 3 个 RadioBoxe 和一个滑块。这就是我得到的:

我希望 3 RadioBox 在同一行并在滑块下方显示的问题。有可能吗?

我希望滑块只通过奇数 (1,3,5,7,9 ...)

这是我目前的代码:

window = Tk()
window.title("Welcome to LikeGeeks app")

panelA = None

menu = Menu(window)
new_item = Menu(menu)
new_item.add_command(label='Open File', command=open_file)
new_item.add_separator()
new_item.add_command(label='Save File')
new_item.add_separator()
new_item.add_command(label='Exit')
menu.add_cascade(label='File', menu=new_item)
window.config(menu=menu)

label_blur = Label(window, text="Blurring")
label_blur.pack(anchor="w", padx=10, pady=10)
blur_rad1 = Radiobutton(window, text='Average', value=0)
blur_rad2 = Radiobutton(window, text='Gaussian', value=1)
blur_rad3 = Radiobutton(window, text='Median', value=2)
blur_rad1.pack(anchor="w", padx=10, pady=0)
blur_rad2.pack(anchor="w", padx=10, pady=0)
blur_rad3.pack(anchor="w", padx=10, pady=0)
blur_slide = Scale(window, from_=1, to=31, length=600,tickinterval=5, orient=HORIZONTAL)
blur_slide.pack(anchor="w", padx=10, pady=10)

window.mainloop()

【问题讨论】:

  • @stovfl: 不是我的问题是布局,RadioBox 很容易修复

标签: python tkinter radio-button slider


【解决方案1】:

有两种简单的解决方案:使用grid 以便您可以指定行和列,或者继续使用pack,但将单选按钮放在单独的框架中。

既然你已经在用pack了,用第二种方法最简单。

为按钮创建一个框架,并使用pack 将其添加到您希望它出现的窗口中:

button_frame = Frame(window)
button_frame.pack(side="top", fill="x")

使按钮成为框架的子级:

blur_rad1 = Radiobutton(button_frame, ...)
blur_rad2 = Radiobutton(button_frame, ...)
blur_rad3 = Radiobutton(button_frame, ...)

使用pack 从左到右布置按钮:

blur_rad1.pack(side="left", expand=True)
blur_rad2.pack(side="left", expand=True)
blur_rad3.pack(side="left", expand=True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-18
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    相关资源
    最近更新 更多