Discuss / Python / 痴迷于tkinter

痴迷于tkinter

Topic source
from tkinter import *
import math


def quadratic():
        """算一元二次方程的根"""
        A = a.get()
        B = b.get()
        C = c.get()
        A = float(A)
        B = float(B)
        C = float(C)
        X1 = StringVar()
        X2 = StringVar()
        if (B**2 - 4*A*C) < 0:
            X1.set("erron")
            X2.set("erron")
            x1['textvariable'] = X1
            x2['textvariable'] = X2
        else:
            tep1 = (math.sqrt(B ** 2 - 4 * A * C) - B) / (A * 2)
            tep2 = -(math.sqrt(B ** 2 - 4 * A * C) + B) / (A * 2)
            X1.set("%.3f" % tep1)
            X2.set("%.3f" % tep2)
            x1['textvariable'] = X1
            x2['textvariable'] = X2
            # print("x1=%.3f x2=%.3f" % (X1, X2))

    root = Tk()
    a = Entry(root, justify=CENTER)
    b = Entry(root, justify=CENTER)
    c = Entry(root, justify=CENTER)
    lab1 = Label(root, text="x^2 + ")
    lab2 = Label(root, text="x + ")
    x1 = Entry(root, justify=CENTER)
    x2 = Entry(root, justify=CENTER)
    bu = Button(root, text="计算", command=quadratic)
    a.grid(row=0, column=1)
    lab1.grid(row=0, column=2)
    b.grid(row=0, column=3)
    lab2.grid(row=0, column=4)
    c.grid(row=0, column=5)
    bu.grid(row=1, column=3)
    x1.grid(row=2, column=2)
    x2.grid(row=2, column=4)
    root.mainloop()

  • 1

Reply