为什么我无法获得tkinter变量的值?

时间:2019-06-03 11:57:43

标签: python-3.x tkinter

我正在尝试在耦合振荡器仿真中添加配置窗口。但是,当我想获取tkinter变量的值来设置系统时,我只会得到”。

我试图将类型从tk.DoubleVar()更改为tk.StringVar(),但似乎无济于事...

这里是一个对象的配置框架的代码。然后创建其中两个,并将它们添加到配置框架中。

import tkinter as tk


class MassConfig(tk.Frame):

    def __init__(self, root, **kwargs):
        tk.Frame.__init__(self, root, kwargs)
        self.grid()

        # Variables :
        self.weight = tk.StringVar()
        self.vitesse = tk.StringVar()
        self.position = tk.StringVar()

        self.lbl_weight = tk.Label(self, text='Masse :')
        self.lbl_weight.grid(row=0, column=0)
        self.entry_weight = tk.Entry(self, textvariable=self.weight, width=5)
        self.entry_weight.grid(row=0, column=1)

        self.lbl_vit = tk.Label(self, text='Vitesse :')
        self.lbl_vit.grid(row=1, column=0, pady=5)
        self.entry_vit = tk.Entry(self, textvariable=self.vitesse, width=5)
        self.entry_vit.grid(row=1, column=1)

        self.lbl_pos = tk.Label(self, text='Position :')
        self.lbl_pos.grid(row=2, column=0)
        self.entry_pos = tk.Entry(self, textvariable=self.position, width=5)
        self.entry_pos.grid(row=2, column=1)

这里是配置框架的代码,其中有两个“ MassConfig”框架。

import tkinter as tk
from gui.masse_config import MassConfig


class Configuration(tk.Frame):

    def __init__(self, root, **kwargs):
        tk.Frame.__init__(self, root, **kwargs)
        self.root = root
        self.grid()
        self.update()

        self.is_alive = True

        self.data = {}
        self.m1_frame = tk.LabelFrame(self, text='Masse 1', width=200, height=200)
        self.m1_frame.grid(column=0, row=0, padx=(10, 5), pady=(10, 10))

        self.m2_frame = tk.LabelFrame(self, text='Masse 2', width=200, height=200)
        self.m2_frame.grid(column=1, row=0, padx=(5, 10), pady=(10, 10))

        self.config_1 = MassConfig(self.m1_frame)
        self.config_2 = MassConfig(self.m2_frame)
        self.config_1.grid(padx=10, pady=10)
        self.config_2.grid(padx=10, pady=10)

        self.btn_validation = tk.Button(self, text='Valider', command=self.validation)
        self.btn_validation.grid(row=3, column=1, padx=10, pady=(0, 10), sticky=tk.E)

        self.update()

    def validation(self):

        print(":", self.config_1.position.get())

        self.data['Mass 1'] = {}
        self.data['Mass 1']['Position'] = self.config_1.position.get()
        self.data['Mass 1']['Vitesse'] = self.config_1.vitesse.get()
        self.data['Mass 1']['Masse'] = self.config_1.weight.get()

        self.data['Mass 2'] = {}
        self.data['Mass 2']['Position'] = self.config_2.position.get()
        self.data['Mass 2']['Vitesse'] = self.config_2.vitesse.get()
        self.data['Mass 2']['Masse'] = self.config_2.weight.get()

        self.is_alive = False
        self.root.destroy()

我想从字典中的MassConfig类获得tkinter变量self.weight,self.vitesse和self.position的值(来自Configuration类的self.data)。但是除了“。”以外什么都没出来。

0 个答案:

没有答案