if / else语句上的随机tkinter窗口打开

问题描述 投票:-1回答:1

我想知道是否我的if else语句错误,或者它是否是tkinter问题。我想要这样,如果在任何或所有框中都留有0,它会给出错误消息。但是在关闭错误消息后,它将打开一个随机的空白窗口。这是我的代码。特定区域是函数valueget()

中的if else语句。
def mainwindow():    
mainwindow = tk.Tk()
mainwindow.title('Enter values')
mainwindow.geometry('160x110')
mainwindow.config(bg='#aaf0d1')

tk.Label(mainwindow, text = 'Enter a', font = ('verdana'),  bg='#aaf0d1').grid(row=0)
tk.Label(mainwindow, text = 'Enter b', font = ('verdana'),  bg='#aaf0d1').grid(row=1)
tk.Label(mainwindow, text = 'Enter c', font = ('verdana'),  bg='#aaf0d1').grid(row=2)

getA = IntVar()
aBox = tk.Entry(mainwindow, textvariable = getA, width=3,  bg='#aaf0d1')
aBox.grid(row=0, column=1)
aBox.config(highlightbackground='#aaf0d1')                

getB = IntVar()
bBox = tk.Entry(mainwindow, textvariable = getB, width=3,  bg='#aaf0d1')
bBox.grid(row=1, column=1)
bBox.config(highlightbackground='#aaf0d1')

getC = IntVar()
cBox = tk.Entry(mainwindow, textvariable = getC, width=3,  bg='#aaf0d1')
cBox.grid(row=2, column=1)
cBox.config(highlightbackground='#aaf0d1')



button = tk.Button(mainwindow, text='Obtain roots', command = lambda: valueget(), font = ('verdana'), highlightbackground='#aaf0d1')
button.grid(row=4)
button.config(bg='#aaf0d1')

def valueget():    

    readA = getA.get()
    readB = getB.get()
    readC = getC.get()


    intA = int(readA)
    intB = int(readB)
    intC = int(readC)

    negroot = (readB**2)-(4*readA*readC)

    quadformulaplus = (-readB + (pow(negroot,0.5)))/(2*readA) #quad forumla
    quadformulaminus = (-readB - (pow(negroot,0.5)))/(2*readA) #quad forumla



    messagewindow = tk.Tk()
    messagewindow.geometry('290x50')
    messagewindow.title('Roots of the equation')
    messagewindow.config(bg='#aaf0d1')


    if readA == 0 or readB==0 or readC==0 or (readA==0 and readB==0 and readC==0):
        errorwindow = tk.messagebox.showerror(message='none').pack()
    else:                  
        label = tk.Label(messagewindow, text = f'The roots are {quadformulaplus:.1f} and {quadformulaminus:.1f}', bg='#aaf0d1', font = ('verdana'))
        label.grid(row=1)
        closebutton = tk.Button(messagewindow, text='Close', command = lambda: messagewindow.destroy(), font = ('verdana'), highlightbackground='#aaf0d1')
        closebutton.grid(row=2)
        closebutton.config(bg='#aaf0d1')

        messagewindow.mainloop()

   # print(f'the roots are {quadformulaplus:.1f} and {quadformulaminus:.1f}')
mainwindow.mainloop()


def startup():
startpage = tk.Tk()
startpage.title('Solver')
photo = tk.PhotoImage(file = r"/Users/isa/Desktop/DiffEqns/cover.png")  #image load
coverbutton = tk.Button(startpage, image = photo, command = lambda: [startpage.destroy(), mainwindow()])
coverbutton.pack()
coverbutton.configure(highlightbackground='#aaf0d1')


startpage.mainloop()
startup()
python tkinter
1个回答
-1
投票

我建议提高if-else语句的可读性。

coefficients = [readA, readB, readC]
if sum(coefficients):        # If they all are all zeros this will be False
    if min(coefficients):    # If any one is zero, this will be False
        label = tk.Label(messagewindow, text = f'The roots are {quadformulaplus:.1f} and {quadformulaminus:.1f}', bg='#aaf0d1', font = ('verdana'))
        label.grid(row=1)
        closebutton = tk.Button(messagewindow, text='Close', command = lambda: messagewindow.destroy(), font = ('verdana'), highlightbackground='#aaf0d1')
        closebutton.grid(row=2)
        closebutton.config(bg='#aaf0d1')
    else:
        errorwindow = tk.messagebox.showerror(message='none').pack()
else:
    errorwindow = tk.messagebox.showerror(message='none').pack()
© www.soinside.com 2019 - 2024. All rights reserved.