TKinter数学输出到canvas.create_text

时间:2013-11-16 00:51:31

标签: python canvas tkinter

我正在尝试计算公式并将其输出显示为TKinter中的表格。由于它不起作用,我只是想获得一个简单的结果并将其打印到画布小部件。当这个工作我将完成整个贷款公式。因为它是我在GUI或控制台中没有输出。 这甚至可以将计算结果作为文本放在canvas.create_text?

from tkinter import * # Import tkinter

width = 500
height = 500

class MainGUI:
    def __init__(self):
        window = Tk() # Create a window
        window.title(" Loan Schedule ") # Set title

        frame1 = Frame(window)
        frame1.grid(row = 1, column = 1)

        Label(frame1, text = " Loan Amount ").grid(row = 1, column = 1, sticky = W)
        self.v1 = StringVar()
        Entry(frame1, textvariable = self.v1, justify = RIGHT).grid(row = 1, column = 2)

        Label(frame1, text = " Years ").grid(row = 1, column = 3, sticky = W)
        self.v2 = StringVar()
        Entry(frame1, textvariable = self.v2, justify = RIGHT).grid(row = 1, column = 4)

        btCalculate = Button(frame1, text = " Calculate ", command = self.calculate()).grid(row = 1, column = 5, sticky = E)

        frame2 = Frame(window)
        frame2.grid(row = 2, column = 1)

        self.canvas = Canvas(frame2, width = width, height = height, bg = "white")
        self.canvas.pack()

        self.canvas.create_text(25, 25, text = self.calculate(), tags = "text")       

        window.mainloop() # Create an event loop

    def calculate(self):
        result = self.v1.get() + self.v2.get()
        print(result)
        return result

MainGUI()

1 个答案:

答案 0 :(得分:0)

command需要函数名称不带()

command = self.calculate

所以现在可行了

from tkinter import * # Import tkinter

width = 500
height = 500

class MainGUI:
    def __init__(self):
        window = Tk() # Create a window
        window.title(" Loan Schedule ") # Set title

        frame1 = Frame(window)
        frame1.grid(row = 1, column = 1)

        Label(frame1, text = " Loan Amount ").grid(row = 1, column = 1, sticky = W)
        self.v1 = StringVar()
        Entry(frame1, textvariable = self.v1, justify = RIGHT).grid(row = 1, column = 2)

        Label(frame1, text = " Years ").grid(row = 1, column = 3, sticky = W)
        self.v2 = StringVar()
        Entry(frame1, textvariable = self.v2, justify = RIGHT).grid(row = 1, column = 4)

        btCalculate = Button(frame1, text = " Calculate ", command = self.calculate).grid(row = 1, column = 5, sticky = E)

        frame2 = Frame(window)
        frame2.grid(row = 2, column = 1)

        self.canvas = Canvas(frame2, width = width, height = height, bg = "white")
        self.canvas.pack()

        self.canvas.create_text(55, 10, text = self.add_text(), tags = "text")       

        window.mainloop() # Create an event loop

    def calculate(self):
        result = int(self.v1.get()) + int(self.v2.get())
        self.canvas.create_text(25, 25, text = result, tags = "text")          
        print(result)
        return result

    def add_text(self):
        return "HELLO WORLD"

MainGUI()

顺便说一下:以下行表示 - 运行self.calculate()并将结果分配给command

command = self.calculate()