在开始时定义了全局函数,但是它需要尚未定义的变量

时间:2019-02-09 01:10:31

标签: python tkinter tkinter-entry

我一直在使用上的Interface()开发bhaskara方程求解器。我看了一些教程,并将页面分为几类。此应用程序的基本思想如下:

它从StartPage开始,用户单击选择值按钮,然后打开PageOne。用户应在此页面上键入所需的值,然后单击选择值按钮(很抱歉,如果您知道如何将所有值保存在一起,我找不到一种方法。这也请让我知道)。用户保存值之后,他返回StartPage并单击 Calculate 按钮。如果所有值都正确,它将显示result变量。

import tkinter as tk
from tkinter import ttk, END
import math

LARGE_FONT =("Verdana", 12)

def calculate():
    global value_a
    global value_b
    global value_c
    value_list = [value_a, value_b, value_c]
    if "" in value_list:
        return False
    else:
        delta = (int(value_b.get())**2) - 4*int(value_a.get())*int(value_c.get())

        if delta >= 0:
            delta_root = math.sqrt(delta)
            bhask_pos = int(-value_b.get()) + (delta_root)/2*int(value_a.get())
            bhask_neg = int(-value_b.get()) - (delta_root)/2*int(value_a.get())
            global result
            result = "The equation", int(value_a.get()), "x² +", int(value_b.get()), "x +", int(value_c.get()), "has the results ", int(bhask_pos), "and ", int(bhask_neg)
        else:
            pass
        return True


class App(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self,*args, **kwargs)
        #self.geometry("720x360")
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_columnconfigure(0, weight=1)
        container.grid_rowconfigure(0, weight=1)

        self.frames = {}

        for F in (StartPage, PageOne):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()


class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        button = ttk.Button(self, text="Insert values", command=lambda: controller.show_frame(PageOne))
        button.pack(side="top", padx=10, pady=20, expand=False)
        canvas = tk.Canvas(self, width=400, height=200, bg="#C0C0C0", bd="10")
        canvas.pack(side="bottom", padx=10, pady=20, expand=False)
        if calculate() == False:
            canvas.create_text(30, 30, text="Error. Check if you selected all values")
        elif calculate() == True:
            canvas.create_text(30, 30, text=result)
        else:
            pass
        calculation_button = ttk.Button(self, text="Calculate", command=calculate)
        calculation_button.pack()


class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        def get_entry_data_a():
            global value_1
            value_1 = int(value_a.get())
            entry_a.delete(0, END)
            print(value_1)#just for debugging

        def get_entry_data_b():
            global value_2
            value_2 = int(value_b.get())
            entry_b.delete(0, END)
            print(value_2)

        def get_entry_data_c():
            global value_3
            value_3 = int(value_c.get())
            entry_c.delete(0, END)
            print(value_3)

        def event_data_a(event):
            value_1 = int(value_a.get())
            entry_a.delete(0, END)
            print(value_1)

        def event_data_b(event):
            value_2 = int(value_b.get())
            entry_b.delete(0, END)
            print(value_2)

        def event_data_c(event):
            value_3 = int(value_c.get())
            entry_c.delete(0, END)
            print(value_3)

        text_a = tk.Label(self, text="value from a:", padx=10, pady=10)
        text_a.grid(row=1, column=1)
        text_b = tk.Label(self, text="value from b:", padx=10, pady=10)
        text_b.grid(row=2, column=1)
        text_c = tk.Label(self, text="value from c", padx=10, pady=10)
        text_c.grid(row=3, column=1)

        value_a = tk.IntVar()
        entry_a = tk.Entry(self, textvariable=value_a)
        entry_a.grid(row=1, column=2)
        entry_a.delete(0, END)
        button_a = ttk.Button(self, text="Save value", command=get_entry_data_a)
        button_a.grid(row=1, column=3, padx=10, pady=10)

        value_b = tk.IntVar()
        entry_b = tk.Entry(self, textvariable=value_b)
        entry_b.grid(row=2, column=2)
        entry_b.delete(0, END)
        button_b = ttk.Button(self, text="Save value", command=get_entry_data_b)
        button_b.grid(row=2, column=3, padx=10, pady=10)

        value_c = tk.IntVar()
        entry_c = tk.Entry(self, textvariable=value_c)
        entry_c.grid(row=3, column=2)
        entry_c.delete(0, END)
        button_c = ttk.Button(self, text="Save value", command=get_entry_data_c)
        button_c.grid(row=3, column=3,padx=10, pady=10)

        entry_a.bind("<Return>", event_data_a)
        entry_b.bind("<Return>", event_data_b)
        entry_c.bind("<Return>", event_data_c)

        back_button = ttk.Button(self, text="Return to Start Page", command=lambda:controller.show_frame(StartPage))
        back_button.grid(row=5, column=2, padx=20, pady=20)


app = App()
app.mainloop()

结果应显示在canvas中,如果用户未选择值,则显示错误消息。我的实际问题是canvas对象是在StartPage中创建的,因此我需要在StartPage类之前定义函数(因为我需要从函数{{1 }}在画布上创建文本),但用户选择的实际值仅出现在代码的末尾。如何使用这些值?在这种情况下,我应该在哪里定义函数calculate()

这是错误消息:

calculate()

1 个答案:

答案 0 :(得分:0)

要使全局变量正常工作,只需确保在首次使用它们之前为其提供初始值。最简单的方法是在模块级别为其分配默认值。例如:

value_a = ""
value_b = ""
value_c = ""

def calculate():
    global value_a
    global value_b
    global value_c

    # ...

那应该可以使您的代码正常工作。但是,尽管Python支持全局变量,但它们几乎永远不是正确的选择。查看您的应用程序的逻辑,并思考更多使用局部作用域的方法。一种可能性是将value_avalue_bvalue_c设置为App类中的实例变量,因为在两个框架中都可以将controller进行访问。

例如:

def calculate(value_a, value_b, value_c):
    value_list = [value_a, value_b, value_c]
    # ...

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

        self.value_a = ""
        self.value_b = ""
        self.value_c = ""

        # ...

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        # ...

        if calculate(controller.value_a, controller.value_b, controller.value_c):
            # ...

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        def get_entry_data_a():
            value_1 = int(controller.value_a.get())
            entry_a.delete(0, END)
            print(value_1)#just for debugging

        # ...

        controller.value_a = tk.IntVar()