python开发_tkinter_获取单选菜单值

在之前的blog中有提到python的tkinter中的菜单操作

python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐

python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐(二)

python开发_tkinter_菜单选项中英文切换_菜单选项不可用操作_博主推荐

python开发_tkinter_复选菜单

python开发_tkinter_单选菜单_不可用菜单操作

python开发_tkinter_多级子菜单

下面是tkinter的获取单选菜单值的操作

运行效果:

当点击'print party and flavor'按钮的时候,获取单选菜单的值

==========================================================

代码部分:

==========================================================

  1 from tkinter import *
  2 
  3 #       The way to think about this is that each radio button menu
  4 #       controls a different variable -- clicking on one of the
  5 #       mutually exclusive choices in a radiobutton assigns some value
  6 #       to an application variable you provide. When you define a
  7 #       radiobutton menu choice, you have the option of specifying the
  8 #       name of a varaible and value to assign to that variable when
  9 #       that choice is selected. This clever mechanism relieves you,
 10 #       the programmer, from having to write a dumb callback that
 11 #       probably wouldn't have done anything more than an assignment
 12 #       anyway. The Tkinter options for this follow their Tk
 13 #       counterparts:
 14 #       {"variable" : my_flavor_variable, "value" : "strawberry"}
 15 #       where my_flavor_variable is an instance of one of the
 16 #       subclasses of Variable, provided in Tkinter.py (there is
 17 #       StringVar(), IntVar(), DoubleVar() and BooleanVar() to choose
 18 #       from)
 19 
 20 __author__ = {'name' : 'Hongten',
 21               'mail' : 'hongtenzone@foxmail.com',
 22               'blog' : 'http://www.cnblogs.com/',
 23               'QQ': '648719819',
 24               'created' : '2013-09-11'}
 25 
 26 def makePoliticalParties(var):
 27     # make menu button
 28     Radiobutton_button = Menubutton(mBar, text='Political Party',
 29                                     underline=0)
 30     Radiobutton_button.pack(side=LEFT, padx='2m')
 31 
 32     # the primary pulldown
 33     Radiobutton_button.menu = Menu(Radiobutton_button)
 34 
 35     Radiobutton_button.menu.add_radiobutton(label='Republican',
 36                                             variable=var, value=1)
 37 
 38     Radiobutton_button.menu.add('radiobutton', {'label': 'Democrat',
 39                                                 'variable' : var,
 40                                                 'value' : 2})
 41 
 42     Radiobutton_button.menu.add('radiobutton', {'label': 'Libertarian',
 43                                                 'variable' : var,
 44                                                 'value' : 3})
 45 
 46     var.set(2)
 47 
 48     # set up a pointer from the file menubutton back to the file menu
 49     Radiobutton_button['menu'] = Radiobutton_button.menu
 50 
 51     return Radiobutton_button
 52 
 53 
 54 def makeFlavors(var):
 55     # make menu button
 56     Radiobutton_button = Menubutton(mBar, text='Flavors',
 57                                     underline=0)
 58     Radiobutton_button.pack(side=LEFT, padx='2m')
 59 
 60     # the primary pulldown
 61     Radiobutton_button.menu = Menu(Radiobutton_button)
 62 
 63     Radiobutton_button.menu.add_radiobutton(label='Strawberry',
 64                                             variable=var, value='Strawberry')
 65 
 66     Radiobutton_button.menu.add_radiobutton(label='Chocolate',
 67                                             variable=var, value='Chocolate')
 68 
 69     Radiobutton_button.menu.add_radiobutton(label='Rocky Road',
 70                                             variable=var, value='Rocky Road')
 71 
 72     # choose a default
 73     var.set("Chocolate")
 74 
 75     # set up a pointer from the file menubutton back to the file menu
 76     Radiobutton_button['menu'] = Radiobutton_button.menu
 77 
 78     return Radiobutton_button
 79 
 80 
 81 def printStuff():
 82     print("party is", party.get())
 83     print("flavor is", flavor.get())
 84     print()
 85 
 86 #################################################
 87 #### Main starts here ...
 88 root = Tk()
 89 
 90 
 91 # make a menu bar
 92 mBar = Frame(root, relief=RAISED, borderwidth=2)
 93 mBar.pack(fill=X)
 94 
 95 # make two application variables,
 96 # one to control each radio button set
 97 party = IntVar()
 98 flavor = StringVar()
 99 
100 Radiobutton_button = makePoliticalParties(party)
101 Radiobutton_button2 = makeFlavors(flavor)
102 
103 # finally, install the buttons in the menu bar.
104 # This allows for scanning from one menubutton to the next.
105 mBar.tk_menuBar(Radiobutton_button, Radiobutton_button2)
106 
107 b = Button(root, text="print party and flavor", foreground="red",
108            command=printStuff)
109 b.pack(side=TOP)
110 
111 root.title('menu demo')
112 root.iconname('menu demo')
113 
114 root.mainloop()

参考资料:

http://www.oschina.net/code/explore/Python-3.1.3/Demo/tkinter/matt/two-radio-groups.py

转载于:https://www.cnblogs.com/hongten/p/hongten_python_tkinter_radio.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python Tkinter模块提供了一个用户友好的图形用户界面(GUI)框架,可以用于创建简单的GUI应用程序,这些应用程序可以跨平台运行,所以TkinterPython GUI编程的一种流行的选择。TkinterPython的标准GUI库,它包含了很多常见的基本控件,如标签、按钮、文本框、滑块、菜单、复选框、单选钮等,同时Tkinter也支持画布、弹出窗口、消息框等。 Python Tkinter模块具有很多好处。首先,它易于学习和编写。它的语法简单明了,清晰易懂,甚至对于没有任何编程经验的人也很容易理解。其次,有很多教程和文档可供参考,许多初学者和专业开发人员都能从中受益。第三,它是免费的,可以在大多数操作系统上自由使用。 Tkinter的一个非常重要的特性是它的跨平台性。无论是在Windows、Mac还是Linux,Tkinter都能够很好地运行。这大大简化了Python GUI应用程序开发的过程,因为开发人员不必担心在不同平台上的兼容性问题。 Tkinter还非常灵活,它支持很多定制化选项。开发人员可以定制控件的颜色、字体、大小、位置、样式等,以实现自己的需求。此外,Tkinter还支持GUI应用程序的国际化和本地化,使得应用程序可以在不同的语言环境中运行。 总之,Python Tkinter模块是一个灵活、易学、易用的GUI框架,适用于初学者和专业开发人员。使用Tkinter,你可以轻松创建GUI应用程序,并且其跨平台能力使得其使用范围非常广泛。随着Python的不断发展,预计Tkinter也将持续改进和升级,成为更加强大和灵活的GUI框架。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值