如何在python中水平放置单选按钮 [英] How to place radiobuttons horizontal in python

查看:102
本文介绍了如何在python中水平放置单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AANTAL = [(1,"1"),(2,"2"),(3,"3"),(4,"4"),(5,"5"),(6,"6"),]
v= StringVar()
v.set("1")

for text, mode in AANTAL:
but = Radiobutton(Main,padx=20, pady=10,font=('arial', 20, "bold"), bd=4, 
text=text, variable=v, value=mode, indicatoron=0)
but.grid()    

上面的代码显示了一些编号为 1 到 6 的单选按钮.但是,它垂直显示而不是水平显示.有谁知道我该如何解决这个问题?

Above code shows some radiobuttons numbered 1 to 6. However, it displays them vertically instead of horizontally. Does anyone know how I could fix this?

我已经尝试将 row=0 放在 grid 命令中,但这只会将按钮堆叠在一起,而不是将它们散布成一行.

I already tried putting row=0 in the grid command but this only stacks the buttons on top of each other instead of spreading them out over a row.

推荐答案

grid 有两个选项用于放置小部件.row and column.您需要同时指定两者.

grid has two options for placing a widget. row and column. You need to specify both.

buttons = []
vars = []
for idx, (text, mode) in enumerate(AANTAL):
    vars.append(StringVar(value="1"))
    buttons.append(Radiobutton(Main,padx=20, pady=10,font=('arial', 20, "bold"), bd=4, text=text, variable=vars[-1], value=mode, indicatoron=0))
    buttons[-1].grid(row=0, column=idx) 

此外,当使用循环创建小部件时,最好将它们存储在列表中,因为您可以稍后在程序中访问它们.

Also, when using loops to create widgets, it is much better to store them in a list because you can access them later in your program.

这篇关于如何在python中水平放置单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆