简体   繁体   English

python 错误修正:各种 tkinter 选项菜单选择相同的选项

[英]python bugfix: various tkinter option menus pick same option

I"m writing a GUI in Python for an interface for an microcontroller board which enables the user to select the kind of sensor for each channel to be read using tkinter. Since there are a few of them, I wanted to set them up by a loop. Now the problem is, whenever I choose an option for one widget, the other ones will pick that option, too. Obviously I want to be able to pick a different option for each channel.我正在 Python 中为微控制器板的接口编写一个 GUI,该接口使用户能够使用 select 使用 ZE5BA8B4C39C29BF13426EF5E0287IAAA5Z 读取每个通道的传感器类型,因为有几个需要设置它们。循环。现在的问题是,每当我为一个小部件选择一个选项时,其他小部件也会选择该选项。显然我希望能够为每个频道选择不同的选项。

import tkinter as tk
import numpy as np

root = tk.Tk()


class Window(tk.Frame):

    def __init__(self, root): 
        self.root = root 
        tk.Frame.__init__(self, root)
        self.root.title("Einstellungen")
        self.root.geometry(newGeometry="320x200")
        self.pack()

        self.bg = tk.Canvas(self.root)
        self.bg.pack()

        analogChannelsIn = tk.LabelFrame(self.root, text="Input", width=100)
        analogChannelsIn.pack()
        self.AnaOptions = np.array([[["-None-"], ["K-type Thermocouple"], ["rH Sensor"]] * 7], 'object')
        self.AnaOptions = self.AnaOptions.reshape([7, 3])
        self.addm = np.array([tk.StringVar()] * 7, 'object')
        self.acdd = np.zeros(7, 'object')
        for i in range(0, 7, 1):
            self.acdd[i] = tk.OptionMenu(analogChannelsIn, self.addm[i],*self.AnaOptions[i])
            self.addm[i].set("-None-")
            self.acdd[i].pack()

settings = Window(root)

settings.mainloop()

The issue is the line:问题是这一行:

self.addm = np.array([tk.StringVar()] * 7, 'object')

because [tk.StringVar()] * 7 creates a list of 7 times the same StringVar .因为[tk.StringVar()] * 7创建了一个包含 7 次相同StringVar的列表。 To get 7 different StringVar , use要获得 7 个不同的StringVar ,请使用

self.addm = np.array([tk.StringVar() for i in range(7)], 'object')

instead.反而。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM