【问题标题】:How to use the .get() function with a Radio Button如何将 .get() 函数与单选按钮一起使用
【发布时间】:2012-12-25 00:40:33
【问题描述】:

我正在尝试使用 RadioButton 制作一个基本的绘图程序来确定画笔的形状。

self.choseo = Radiobutton(text='Circle', variable=shape,indicatoron=0, value=1)
self.choser = Radiobutton(text='Rectangle', variable=shape,indicatoron=0, value=2)
self.chosea = Radiobutton(text='Arc', variable=shape,indicatoron=0, value=3)

对应于:

   if shape.get()==3:
      self.display.create_arc( self.x1, self.y1, self.x2,
          self.y2, fill = mycolor, outline= mycolor, tags = "line")
   elif shape.get()==2:
      self.display.create_rectangle( self.x1, self.y1, self.x2,
          self.y2, fill = mycolor, outline= mycolor, tags = "line")
   elif shape.get()==1:
      self.display.create_oval( self.x1, self.y1, self.x2,
          self.y2, fill = mycolor, outline= mycolor, tags = "line")

当我运行这个时,我得到这个错误:

"TypeError: get() takes exactly 1 argument (0 given)"

我该如何进行这项工作?

【问题讨论】:

  • 什么是形状?通常 .get() 接受一个索引。

标签: python radio-button tkinter


【解决方案1】:

您没有说出shape 是什么,但您应该确保使用IntVar 的实例。

试试下面的代码:

from Tkinter import *
master = Tk()
shape = IntVar() # ensure you use an instance of IntVar
Radiobutton(text='Circle', variable=shape, indicatoron=0, value=1, master=master).pack()
Radiobutton(text='Rectangle', variable=shape, indicatoron=0, value=2, master=master).pack()
Radiobutton(text='Arc', variable=shape, indicatoron=0, value=3, master=master).pack()

shape.get() 会按照你想要的方式工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-20
    • 2010-09-25
    • 2010-12-07
    • 1970-01-01
    • 2014-01-26
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    相关资源
    最近更新 更多