python - 在获取选择了哪个单选按钮并根据选择发出命令时遇到一些问题

标签 python tkinter

我正在编写一个将图像转换为灰度的程序。我让它工作正常,现在我正在实现单选按钮,让用户选择要使用的灰度类型。到目前为止,我的问题是,当第一次创建单选按钮时,立即调用 command = function 并将所有 bool 值设置为 True,因为这就是我通过使用 function() 而不是 function 传递来明确告诉它要做的事情。我正在尝试想出一种方法来存储选择的单选按钮,或者希望有内置的东西可以让我进行检查。我确实知道使用全局变量不是最佳实践,并且类将消除它们的必要性。这是相关代码。

# Global variables for radio buttons----
radio1 = False
radio2 = False
radio3 = False
radio4 = False

def whichSelected(numberSelected):
    global radio1
    global radio2
    global radio3
    global radio4
    if numberSelected == 4:
        radio4 = True
    if numberSelected == 3:
        radio3 = True
    if numberSelected == 2:
        radio2 = True
    if numberSelected == 1:
        radio1 = True

# Radio Button Code---------------------------------------------------------
var = tkinter.IntVar()
option1 = tkinter.Radiobutton(window, text ='Average Grayscale',variable = var, value = 1,command =  whichSelected(1))
option2 = tkinter.Radiobutton(window, text ='Lightness Grayscale',variable = var, value = 2, command = whichSelected(2))
option3 = tkinter.Radiobutton(window, text ='Luminosity Grayscale',variable = var, value = 3, command = whichSelected(3))
option4 = tkinter.Radiobutton(window, text ='Invert',variable = var, value = 4, command = whichSelected(4))

def change_pixel():
    global tkPic2
    global radio1
    global radio2
    global radio3
    global radio4
    # Treats the image as a 2d array, iterates through changing the
    #values of each pixel with the algorithm for gray
    rgbList = pic.load() #Get a 2d array of the pixels
    for row in range(picWidth):
        for column in range(picHeight):
            rgb = rgbList[row,column]
            r,g,b = rgb # Unpacks the RGB value tuple per pixel
            if radio1 == True:
                grayAlgorithm1 = grayAverage(r,g,b)
                rgbList[row,column] = (grayAlgorithm1, grayAlgorithm1, grayAlgorithm1)
            elif radio2 == True:
                grayAlgorithm = lightness(r,g,b)
                rgbList[row,column] = (grayAlgorithm1, grayAlgorithm1, grayAlgorithm1)
            elif radio3 == True:
                grayAlgorithm1= luminosity(r,g,b)
                rgbList[row,column] = (grayAlgorithm1, grayAlgorithm1, grayAlgorithm1)     # Gives each pixel a new RGB value
            elif radio4 == True:
                r,g,b= invertRGB(r,g,b)
                rgbList[row,column] = (r, g, b) # Gives each pixel a new RGB value
        # Converting to a tkinter PhotoImage
    tkPic2 = ImageTk.PhotoImage(pic, master = window)
    print(radio1,radio2,radio3,radio4)
    canvas1.create_image(815,170, anchor='e',image = tkPic2)

最佳答案

我不完全确定为什么您首先需要 whichSelected 。您应该能够从 var 的值看出:

value = var.get()
if value == 1:
    print "Average Grayscale"
elif value == 2:
    print "Lightness Grayscale"
...

这还有一个额外的好处,即可以保证您知道当前检查的是哪个值。使用之前的方法,您需要添加一些逻辑,将所有全局变量都设为 False,然后再将其设置为您想要的 True。正如您的函数所示,如果用户选择一个单选按钮,然后选择另一个单选按钮,则两者都将在全局变量中标记为 True

<小时/>

但是,指出您的方法失败的原因是有启发性的。除了前面提到的问题之外,command 参数应该是一个函数,您正在传递函数的结果(在这种情况)。当您调用函数来获取结果时,您将全局变量设置为 True 作为副作用。

快速解决方法是使用 lambda 创建一个新函数,该函数按照您想要的方式调用旧函数。这样,您将推迟将全局变量设置为 True ,直到您的单选按钮被实际单击为止:

option1 = tkinter.Radiobutton(window, text='Average Grayscale',
                                      variable=var,
                                      value=1,
                                      command=lambda: whichSelected(1))

lambda 最终对于 tkinter 应用程序中的此类事情非常有用。 (我无法想象没有它就编写应用程序......)

关于python - 在获取选择了哪个单选按钮并根据选择发出命令时遇到一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20417698/

相关文章:

python - 检查值是否在 DataFrame 系列 ("The truth value of a Series is ambiguous"错误中)

python - “var”是此函数的无效关键字参数? (Tkinter 和 Python)

python - 仅接受一种文件类型

python - 同时执行 Tkinter 和 Shell 命令

tkinter - 如何为 Tk 构建包装器?

python - chrome 上的 DevTools 远程调试无法与 headless-chrome 正常工作

python - 在 `pyarrow` 测试中使用内存文件系统

python - Django DateTimeInput 类型 'datetime-local' 未保存到数据库

python - PhotoImage 中的透明度错误

python - 使用 subprocess.Popen() 打开程序时出错