将Tkinter条目存储到新变量中

时间:2018-07-05 15:04:16

标签: python matplotlib tkinter

我正在编写一个程序,该程序允许用户在文本框中输入特定温度,然后根据用户输入显示相应的图形。我知道该程序在手动写入起始温度时可以运行,但是似乎无法弄清楚如何将用户输入值存储到正确的变量中然后使用。我将整个程序放在下面,但是我的问题出在名为PageTwo的类中。我希望输入的值存储为变量名称“ Tc”,该名称在页面的绘图部分中使用。我不断收到错误“ AttributeError:'PageTwo'对象没有属性'ID1'”。任何帮助将不胜感激。

import tkinter as tk
from tkinter import font as tkfont
from tkinter import *
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure

class SampleApp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.title_font = tkfont.Font(family='Helvetica', weight="bold")
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Temperature Distribution as a function of Distance")
        label.pack(pady=10, padx=10)


        button2 = tk.Button(self, text="Finish & Plot", command=self.plot)
        button2.pack()

        var1 = StringVar()
        var1.set("Enter inner chamber temperature:")
        label1 = tk.Label(self, textvariable=var1, height=2)
        label1.pack()

        ID1 = StringVar()
        entry1 = tk.Entry(self, bd=4, textvariable=ID1)
        entry1.pack()

    def read_inputs(self):
        x = self.entry1.get()

    def plot(self):
        Tc = self.read_inputs()
        f = 49
        h1 = 50
        L1 = .05 
        L2 = .022  
        L3 = .1125
        k_sand = 0.27 
        k_water = 0.6  
        k_brick = 0.72 
        e_sand = 0.343  
        k_ws = e_sand * k_water + (1 - e_sand) * k_sand
        T4 = -((f * (1 / h1)) - Tc)
        T3 = -((f * (L1 / k_al)) - T4)
        T2 = -((f * (L2 / k_ws)) - T3)
        T1 = -((f * (L3 / k_brick)) - T2)

        print(T4, T3, T2, T1)


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

2 个答案:

答案 0 :(得分:0)

首先,您的代码不起作用。阅读this。我建议至少尝试在将代码发布到此处之前运行代码...

您的代码不起作用,因为您尝试在pack类下调用grid的对象中使用self.pack的对象中有PageOne的从属对象。 tkinter绝对不会让您这样做。

然后您说收到的错误是"AttributeError: 'PageTwo' object has no attribute 'ID1'"。为此,您需要在ID1类内用self somewhere 声明变量PageTwo,而不必声明。

在PageTwo类的init方法中,将tkinter Entry小部件声明为entry1

后来您在self.entry1函数中使用read_inputs。所以这是一个简单的解决方法,只需将entry1声明为self.entry1 ...

class PageTwo(tk.Frame):
    def __init__(self, parent, controller):
        ...
        ID1 = StringVar()
        self.entry1 = tk.Entry(self, bd=4, textvariable=ID1)
        self.entry1.pack()
        ...

    def read_inputs(self):
        x = self.entry1.get()

我无法运行您的代码来验证这一点,但是我敢打赌,由于该变量名不正确,您无法读取输入。

答案 1 :(得分:0)

您所描述的根本不是tkinter问题。您只需要使小部件成为类的属性,然后就需要转换并返回read_inputs中的值:

class PageTwo(tk.Frame):
    def __init__(self, parent, controller):
        ...
        self.entry1 = tk.Entry(...)
        ...

    def read_inputs(self):
        x = self.entry1.get()
        return float(x)