Python3 tkinter基础 LabelFrame Radiobutton 形成两组不相互限制的单选按钮

时间:2023-12-23 20:08:13

  •        Python : 3.7.0
  •          OS : Ubuntu 18.04.1 LTS
  •         IDE : PyCharm 2018.2.4
  •       Conda : 4.5.11
  •    typesetting : Markdown

左右布局

code

"""
@Author : 行初心
@Date : 18-10-1
@Blog : www.cnblogs.com/xingchuxin
@Gitee : gitee.com/zhichengjiu
"""
from tkinter import * def main():
root = Tk() group1 = LabelFrame(root, text='服器部', padx=5, pady=5)
group1.pack(side=RIGHT, padx=10, pady=10) v = IntVar()
v.set(3) rb1_group1 = Radiobutton(group, text='锦', variable=v, value=1, indicatoron=False)
rb1_group1.pack(fill=X) rb2_group1 = Radiobutton(group, text='绢', variable=v, value=2, indicatoron=False)
rb2_group1.pack(fill=X) rb3_group1 = Radiobutton(group, text='帛', variable=v, value=3, indicatoron=False)
rb3_group1.pack(fill=X) group2 = LabelFrame(root, text='果部', padx=5, pady=5)
group2.pack(side=LEFT, padx=10, pady=10) v2 = IntVar()
v2.set(2) rb1_group2 = Radiobutton(group2, text='李', variable=v2, value=1, indicatoron=False)
rb1_group2.pack(fill=X) rb2_group2 = Radiobutton(group2, text='杏', variable=v2, value=2, indicatoron=False)
rb2_group2.pack(fill=X) rb3_group2 = Radiobutton(group2, text='梅', variable=v2, value=3, indicatoron=False)
rb3_group2.pack(fill=X) mainloop() if __name__ == '__main__':
main()

result

Python3 tkinter基础 LabelFrame Radiobutton 形成两组不相互限制的单选按钮

上下布局

code

"""
@Author : 行初心
@Date : 18-10-1
@Blog : www.cnblogs.com/xingchuxin
@Gitee : gitee.com/zhichengjiu
"""
from tkinter import * def main():
root = Tk() group1 = LabelFrame(root, text='菜部', padx=5, pady=5)
group1.pack(padx=10, pady=10) v = IntVar()
# v与value的关系
v.set(3) rb1_group1 = Radiobutton(group, text='冬瓜', variable=v,
value=1, indicatoron=False)
rb1_group1.pack(fill=X) rb2_group1 = Radiobutton(group, text='南瓜', variable=v,
value=2, indicatoron=False)
rb2_group1.pack(fill=X) rb3_group1 = Radiobutton(group, text='越瓜', variable=v,
value=3, indicatoron=False)
rb3_group1.pack(fill=X) group2 = LabelFrame(root, text='谷部', padx=5, pady=5)
group2.pack(padx=10, pady=10) v2 = IntVar()
v2.set(2) rb1_group2 = Radiobutton(group2, text='黄大豆', variable=v2,
value=1, indicatoron=False)
rb1_group2.pack(fill=X) rb2_group2 = Radiobutton(group2, text='赤小豆', variable=v2,
value=2, indicatoron=False)
rb2_group2.pack(fill=X) rb3_group2 = Radiobutton(group2, text='刀豆', variable=v2,
value=3, indicatoron=False)
rb3_group2.pack(fill=X) mainloop() if __name__ == '__main__':
main()

result

Python3 tkinter基础 LabelFrame Radiobutton 形成两组不相互限制的单选按钮

reference

  • [文档] docs.python.org/3/library/tkinter.html

resource

  • [文档] docs.python.org/3
  • [规范] www.python.org/dev/peps/pep-0008
  • [规范] zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_language_rules
  • [源码] www.python.org/downloads/source
  • [ PEP ] www.python.org/dev/peps
  • [平台] www.cnblogs.com
  • [平台] gitee.com


Python具有开源、跨平台、解释型和交互式等特性,值得学习。

Python的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。

GUI可以选择PyQt5、PySide2、wxPython、PyGObject、wxWidgets等进行创作。

代码的书写要遵守规范,这样有助于沟通和理解。

每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。