-1

假设我有一个多维列表:

my_list = [[1,2,3,4,5], [2,3,4,5,6], [3,4,5,6,7]]

现在我想用 Tkinter 创建一个 GUI,可以在其中选中复选框来选择哪些子列表应该绘制在直方图中。所以对于这个例子,我想象三个复选框(标记为 0、1、2)和一个Button“显示直方图”。假设我选中了标记为 1 和 2 的复选框并按下“显示直方图”按钮,它应该显示和的直方图my_list[0]my_list[1]最好作为一个画布上的子图)。方法是什么?

4

2 回答 2

2

OOP 示例

定义一个class SubplotCheckbutton ...,继承自tk.Checkbutton
通过以下方式扩展tk.Checkbutton小部件:

  • 命名参数subplot=
  • 所需的tk.Variable,在这里tk.IntVar
  • 根据检查状态checked()返回的类方法。True/False

参考


  1. init 方法中的参数parent和是什么意思?**kwargs
    每个Tkinter小部件都需要一个parent. 因此,所有Tkinter小部件类对象的第一个参数都采用该parent参数。Tkinter 中的父级指定您的小部件在哪个小部件Checkbutton中进行布局。
    • class App(tk.Tk):=>self
    • SubplotCheckbutton(self, ...
    • def __init__(..., parent, ...
    • super().__init__(parent, ...=>tk.Checkbutton(parent)

**kwargs从已知的单词参数中缩短并且类型为dict.
这里:text=str(i)subplot=subplot

  1. 将继续...

import tkinter as tk


class SubplotCheckbutton(tk.Checkbutton):
    def __init__(self, parent, **kwargs):
        # Pop the 'subplot=' argument and save to class member
        self.subplot = kwargs.pop('subplot')

        # Extend this class with the required tk.Variable
        self.variable = tk.IntVar()

        # __init__ the inherited (tk.Checkbutton) class object
        # Pass the argument variable= and all other passed arguments in kwargs
        super().__init__(parent, variable=self.variable, **kwargs)

    # Extend this object with a checked() method
    def checked(self):
        # Get the value from the tk.Variable and return True/False
        return self.variable.get() == 1

用法

注意:不root,类对象App是根对象,因此您必须self用作父对象:

  • SubplotCheckbutton(self, ...
  • Button(self, ...
class App(tk.Tk):
    def __init__(self):
        super().__init__()

        my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
        self.channels = []

        for i, subplot in enumerate(my_list):
            self.channels.append(SubplotCheckbutton(self, text=str(i), subplot=subplot))
            self.channels[-1].pack()

        tk.Button(self, text="Show Histograms", command=self.show).pack()

    def show(self):
        for channel in self.channels:
            if channel.checked():
                fig, ax = plt.subplots()
                y, x, _ = ax2.hist(channel.subplot, bins = 150)
                plt.show()


if __name__ == '__main__':
    App().mainloop()
于 2020-06-23T19:27:25.047 回答
0
root = Tk()

my_list = [[1,2,3,4,5], [2,3,4,5,6], [3,4,5,6,7]]

var = IntVar()
var2 = IntVar()
var3 = IntVar()

def show():
    

    if var.get() == 1: 
        fig, ax = plt.subplots()
        y, x, _ = ax.hist(my_list[0], bins = 150)
        

    if var2.get() == 1:
        fig2, ax2 = plt.subplots()
        y, x, _ = ax2.hist(my_list[1], bins = 150)
        

    if var3.get()    def checked(self):
        return self.variable.get() == 1

 == 1:
        fig3, ax3 = plt.subplots()
        y, x, _ = ax3.hist(my_list[2], bins = 150)
    
    plt.show()





button = Button(root, text = "Show Histograms", command = show).pack()

c = Checkbutton(root, text = 'first list', variable = var).pack()
c2 = Checkbutton(root, text = 'second list', variable = var2).pack()
c3 = Checkbutton(root, text = 'third list', variable = var3).pack()


root.mainloop()

更新:我设法把它写得更紧凑,但它不是这样工作的:

my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
varChannels = []
checkbuttonChannels = []

def show():
    for i in range(3):
        if varChannels[i] == 1:
            fig, ax = plt.subplots()
            y, x, _ = ax2.hist(my_list[i], bins = 150)
            plt.show()



for _ in range(3):
    varChannels.append(IntVar())
    checkbuttonChannels.append('0')

for i in range(3):
    checkbuttonChannels[i] = Checkbutton(root, text = str(i), variable = varChannels[i]).pack()

button = Button(root, text = "Show Histograms", command = show).pack()

root.mainloop()
于 2020-06-23T14:01:07.717 回答