ii6ri6r 2021-04-18 18:00 采纳率: 100%
浏览 59
已采纳

一个Python的GUI为什么出错?

from tkinter import *
import random
from math import *
from tkinter import messagebox

from pefile import b


class Application(Frame):

    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.createWidget()

    def createWidget(self):
        a = StringVar()
        b = StringVar()
        c = StringVar()
        
        self.label01 = Label(self, text="二次项系数:").grid(row=0, column=0)
        self.entry01 = Entry(self, textvariable=a).grid(row=0, column=1)

        self.label02 = Label(self, text="一次项系数:").grid(row=1, column=0)
        self.entry02 = Entry(self, textvariable=b).grid(row=1, column=1)
        
        self.label03 = Label(self, text="常数项系数:").grid(row=2, column=0)
        self.entry03 = Entry(self, textvariable=c).grid(row=2, column=1)

        Button(self, text="计算", command=self.jisuan).grid(row=3, column=1, sticky=EW)
        Button(self, text="退出").grid(row=3, column=2, sticky=E)

        self.label04 = Label(self, text="请先将方程的右端化为0").grid(row=4, sticky=EW)
    
    def jisuan(self):
        aa = self.label01.get()
        bb = self.label01.get()
        cc = self.label01.get()
        print(aa)
        d = int((bb)**2-4*(aa)*(cc))
        if d < 0:
            messagebox.showinfo("计算结果","此二次方程无实数根")
        elif d == 0:
            outcome = -b/(2*aa)
            messagebox.showinfo("计算结果","方程有且仅有一个实数根:%d" % outcome)
        elif d > 0:
            x1 = (-b + sqrt(d))/(2*aa)
            x2 = (-b - sqrt(d))/(2*aa)
            messagebox.showinfo("计算结果","方程有两个实数根,分别为:%d和%d" % (x1, x2))



if __name__=='__main__':
    root=Tk()
    root.geometry("400x300+200+300")
    app=Application(master=root)
    root.mainloop()


input()
  • 写回答

5条回答 默认 最新

  • CSDN专家-HGJ 2021-04-18 18:34
    关注

    改了一下代码,界面运行正常,排除了代码中错误,但是其计算部分你看一下再作调整。

    from tkinter import *
    import random
    from math import *
    from tkinter import messagebox
    
    #from pefile import b
    
    
    class Application(Frame):
    
        def __init__(self, master=None):
            super().__init__(master)
            self.master = master
            self.pack()
            self.createWidget()
    
        def createWidget(self):
            a = StringVar()
            b = StringVar()
            c = StringVar()
    
            self.label01 = Label(self, text="二次项系数:").grid(row=0, column=0)
            self.entry01 = Entry(self, textvariable=a)
            self.entry01.grid(row=0, column=1)
    
            self.label02 = Label(self, text="一次项系数:").grid(row=1, column=0)
            self.entry02 = Entry(self, textvariable=b)
            self.entry02.grid(row=1, column=1)
    
            self.label03 = Label(self, text="常数项系数:").grid(row=2, column=0)
            self.entry03 = Entry(self, textvariable=c)
            self.entry03.grid(row=2, column=1)
    
            Button(self, text="计算", command=self.jisuan).grid(
                row=3, column=1, sticky=EW)
            Button(self, text="退出",command=self.quit).grid(row=3, column=2, sticky=E)
    
            self.label04 = Label(self, text="请先将方程的右端化为0").grid(row=4, sticky=EW)
    
        def jisuan(self):
            aa = self.entry01.get()
            bb = self.entry02.get()
            cc = self.entry03.get()
            print(aa)
            d = float((int(bb)**2-4*(int(aa)*int(cc))))
            if d < 0:
                messagebox.showinfo("计算结果", "此二次方程无实数根")
            elif d == 0:
                outcome = -b/(2*aa)
                messagebox.showinfo("计算结果", "方程有且仅有一个实数根:%d" % outcome)
            elif d > 0:
                x1 = (-b + sqrt(d))/(2*aa)
                x2 = (-b - sqrt(d))/(2*aa)
                messagebox.showinfo("计算结果", "方程有两个实数根,分别为:%d和%d" % (x1, x2))
    
    
    if __name__ == '__main__':
        root = Tk()
        root.geometry("400x300+200+300")
        app = Application(master=root)
        root.mainloop()
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 关于#vscode#的问题:ESP32开发板对接MQTT实现小灯泡的开关
  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏