简体   繁体   English

Tkinter-通过类给比例的变量选项名称

[英]Tkinter - Give name of variable option of a Scale through a class

I am starting to learn OOP and I've been struggling with some basic stuff. 我开始学习OOP,并且一直在努力学习一些基本知识。

In the code below, I have created a class Scales() that I want to use to create 2 very similar scales, with only their variable option being different. 在下面的代码中,我创建了一个Scales()类,该类可用于创建2个非常相似的比例尺,只是它们的variable选项不同。

How can I pass the name of these variables as a parameter when I call Scales() and make both of them a DoubleVar type? 当我调用Scales()并将它们都设置为DoubleVar类型时,如何传递这些变量的名称作为参数?

from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
import numpy as np

class Scales(Frame):
    def __init__(self, parent, variable_name, label_text, initial_value,
                 final_value):

        self.parent        = parent
        self.bar_length    = 200
        self.variable_name = variable_name
        self.label_text    = label_text
        self.initial_value = initial_value
        self.final_value   = final_value

        # self.variable_name = DoubleVar()

        self.scale_name = Scale(self.parent, variable=self.variable_name,
                                orient=HORIZONTAL,
                                from_=self.initial_value,
                                to=self.final_value,
                                length=self.bar_length, cursor="hand",
                                label=self.label_text)


class MainApplication(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.slice_number_scale = Scales(self.parent, slice_number,
                                         "Slice Number", 1, 24)


if __name__ == '__main__':
    root = Tk()
    root.geometry("800x600")
    MainApplication(root)
    root.mainloop()

If the variables are going to live as instance variables of your Scales class, then there's absolutely no reason to give them separate names; 如果这些变量将作为Scales类的实例变量使用,则绝对没有理由给它们单独命名。 every reference to them is going to be in the context of some particular instance. 对它们的每个引用都将在某个特定实例的上下文中进行。 You'd probably want to define a get() method that does something like return self.variable.get() , for the convenience of the class's user. 为了方便类的用户,您可能希望定义一个get()方法,该方法的功能类似于return self.variable.get()

If the variables live somewhere outside the class, then Scales should not care what their names are; 如果变量位于类之外的某个地方,那么Scales不必理会它们的名称。 pass the variable itself as a parameter to the class constructor, and pass it on as the variable= option to Scale() . 变量本身作为参数传递给类构造函数,并将其作为variable=选项传递给Scale()

Just create the variables in each Scale class instance that is created, then access them through the instance's name. 只需在创建的每个Scale类实例中创建变量,然后通过实例的名称访问它们。 Here's what I mean: 这就是我的意思:

from tkinter import *
#from tkinter import ttk
#from PIL import Image, ImageTk
#import numpy as np

class Scale(Frame):
    """ Dummy version of class for testing and illustration. """
    def __init__(self, parent, orient=None, from_=None, to=None, length=None,
                 cursor=None, label=None):
        Frame.__init__(self, parent)  # initialize base class
        self.variable = DoubleVar()  # create variable and make attribute


class Scales(Frame):
    def __init__(self, parent, label_text, initial_value,
                 final_value):

        self.parent        = parent
        self.bar_length    = 200
#        self.variable_name = variable_name
        self.label_text    = label_text
        self.initial_value = initial_value
        self.final_value   = final_value

#        self.variable_name = DoubleVar()

        self.scale1 = Scale(self.parent,
#                            variable=self.variable_name,
                            orient=HORIZONTAL,
                            from_=self.initial_value,
                            to=self.final_value,
                            length=self.bar_length,
                            cursor="hand",
                            label=self.label_text)

        self.scale1.pack()


class MainApplication(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        slice_number = 42
        self.slice_number_scale = Scales(self.parent, slice_number, 1, 24)

root = Tk()
app = MainApplication(root)
app.mainloop()

After doing this you can access the variable for each Scale instance within a Scales instance as self.scale1.variable (and self.scale2.variable after you add it). 完成此操作后,您可以将Scales实例中每个Scale实例的变量作为self.scale1.variable (以及self.scale2.variable后的self.scale2.variable )进行访问。 Within the MainApplication instance they can be referred to as self.slice_number_scale.scale1.variable (and self.slice_number_scale2.variable ). MainApplication实例中,它们可以称为self.slice_number_scale.scale1.variable (和self.slice_number_scale2.variable )。

For the latter you might want to add methods to class MainApplication to make such references more succinct, such as: 对于后者,您可能需要向MainApplication类添加方法,以使此类引用更加简洁,例如:

class MainApplication(Frame):
       ....
    def get_scale_var1(self):
        return self.slice_number_scale.scale1.variable.get()

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

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