显示意外行为的 tkinter 代码

Posted

技术标签:

【中文标题】显示意外行为的 tkinter 代码【英文标题】:tkinter code showing unexpected behavior 【发布时间】:2017-08-16 12:30:33 【问题描述】:

我在使用 tkinter 代码时遇到问题,该代码应该创建一个界面并具有一些按钮。 我有一些错误,但我似乎找不到它们。

我的代码如下:

from tkinter import*
import random
import time;

root = Tk()
root.geometry("1600x800+0+0")
root.title("Restruant Managment System")

text_Input = StringVar()
operator = ""


Tops = Frame(root, width = 1600,height = 50,bg="powder blue", relief=SUNKEN)
Tops.pack(side=TOP)

f1 = Frame(root, width = 800,height = 700, relief=SUNKEN)
f1.pack(side=LEFT)

f2 = Frame(root, width = 300,height = 700,bg="powder blue", relief=SUNKEN)
f2.pack(side=RIGHT)
#===================Time============================
localtime=time.asctime(time.localtime(time.time()))
#======================Info=========================
lblInfo = Label(Tops, font=('SHOWCARD GOTHIC',50, 'bold'),text="Restruant managment system",fg="Steel Blue", bd=10, anchor='w')
lblInfo.grid(row=0,column=0)
lblInfo = Label(Tops, font=('SHOWCARD GOTHIC',20, 'bold'),text=localtime,fg="Steel Blue", bd=10, anchor='w')
lblInfo.grid(row=1,column=0)
#=========================Calculator==============
def btnClick(numbers):
    global operator
    operator = operator + str(numbers)
    text_Input.set(operator)

def btnClearDisplay():
    global operator
    operator=""
    text_Input.set("")

def btnEqualsInput():
    global operator
    sumup =str(eval(operator))
    text_Input.set(sumup)
    operator=""


def Ref():
    x = random.radient(10908, 500876)
    randomRef = str(x)
    rand.set(randomRef)

def qExit():
    root.destroy()

def Reset():
    rand.set("")
    Fries.set("")
    Burger.set("")
    Filet.set("")
    SubTotal.set("")
    Total.set("")
    Service_Charge.set("")
    Drinks.set("")
    Tax.set("")
    Cost.set("")
    Chicken_Burger.set("")
    Cheese_Burger.set("")



txtDisplay = Entry(f2,font=('SHOWCARD GOTHIC',20, 'bold'), textvariable=text_Input, bd=30, insertwidth=4,
                   bg="black", fg="white", justify='right')
txtDisplay.grid(columnspan=4)

#=====================================================================================================================
btn7=Button(f2,padx=16, pady=8,bd=8, fg="black", font=('arial',20,'bold'),
            text="7", bg="powder blue", command=lambda: btnClick(7)).grid(row=2,column=0)

btn8=Button(f2,padx=16, pady=8,bd=8, fg="black", font=('arial',20,'bold'),
            text="8", bg="powder blue", command=lambda: btnClick(8)).grid(row=2,column=1)

btn9=Button(f2,padx=16, pady=8,bd=8, fg="black", font=('arial',20,'bold'),
            text="9", bg="powder blue", command=lambda: btnClick(9)).grid(row=2,column=2)

Addition=Button(f2,padx=16, pady=8,bd=8, fg="black", font=('arial',20,'bold'),
            text="+", bg="powder blue", command=lambda: btnClick("+")).grid(row=2,column=3)
#=====================================================================================================================
btn4=Button(f2,padx=16, pady=8,bd=8, fg="black", font=('arial',20,'bold'),
            text="4", bg="powder blue", command=lambda: btnClick(4)).grid(row=3,column=0)

btn5=Button(f2,padx=16, pady=8,bd=8, fg="black", font=('arial',20,'bold'),
            text="5", bg="powder blue", command=lambda: btnClick(5)).grid(row=3,column=1)

btn6=Button(f2,padx=16, pady=8,bd=8, fg="black", font=('arial',20,'bold'),
            text="6", bg="powder blue", command=lambda: btnClick(6)).grid(row=3,column=2)

Subtraction=Button(f2,padx=16, pady=8,bd=8, fg="black", font=('arial',20,'bold'),
            text="-", bg="powder blue", command=lambda: btnClick("-")).grid(row=3,column=3)
#=====================================================================================================================
btn1=Button(f2,padx=16, pady=8,bd=8, fg="black", font=('arial',20,'bold'),
            text="1", bg="powder blue", command=lambda: btnClick(1)).grid(row=4,column=0)

btn2=Button(f2,padx=16, pady=8,bd=8, fg="black", font=('arial',20,'bold'),
            text="2", bg="powder blue", command=lambda: btnClick(2)).grid(row=4,column=1)

btn3=Button(f2,padx=16, pady=8,bd=8, fg="black", font=('arial',20,'bold'),
            text="3", bg="powder blue", command=lambda: btnClick(3)).grid(row=4,column=2)

Multiply=Button(f2,padx=16, pady=8,bd=8, fg="black", font=('arial',20,'bold'),
            text="*", bg="powder blue", command=lambda: btnClick("*")).grid(row=4,column=3)
#=====================================================================================================================
btn0=Button(f2,padx=16, pady=8,bd=8, fg="black", font=('arial',20,'bold'),
            text="0", bg="powder blue", command=lambda: btnClick(0)).grid(row=5,column=0)

btnC=Button(f2,padx=16, pady=8,bd=8, fg="black", font=('arial',20,'bold'),
            text="C", bg="powder blue", command=btnClearDisplay).grid(row=5,column=1)

btnEquals=Button(f2,padx=16, pady=8,bd=8, fg="black", font=('arial',20,'bold'),
            text="=", bg="powder blue", command=btnEqualsInput).grid(row=5,column=2)

Divide=Button(f2,padx=16, pady=8,bd=8, fg="black", font=('arial',20,'bold'),
            text="/", bg="powder blue", command=lambda: btnClick("/")).grid(row=5,column=3)
#=====================================================================================================================
# restraunt menu
#Restraunt Information 1
rand =StringVar()
Fries =StringVar()
Burger =StringVar()
Filet =StringVar()
SubTotal =StringVar()
Total =StringVar()
Service_Charge =StringVar()
Drinks = StringVar()
Tax =StringVar()
Cost =StringVar()
Chicken_Burger =StringVar()
Cheese_Burger =StringVar()



lblReference = Label(f1,font=('arial', 16,'bold'), text="Reference", bd=16, anchor='w')
lblReference.grid(row=0,column=0)
txtRefernce=Entry(f1,font=('arial', 16,'bold'), textvarible=rand, bd=10, insertwidth=4,
                  bg="powder blue", justify='right')
txtRefernce.grid(row=0,column=1)


lblFries = Label(f1,font=('arial', 16,'bold'), text="Large Fries", bd=16, anchor='w')
lblFries.grid(row=1,column=0)
txtFries=Entry(f1,font=('arial', 16,'bold'), textvarible=Fries, bd=10, insertwidth=4,
                  bg="powder blue", justify='right')
txtFries.grid(row=1,column=1)


lblBurger = Label(f1,font=('arial', 16,'bold'), text="Burger Meal", bd=16, anchor='w')
lblBurger.grid(row=2,column=0)
txtBurger = Entry(f1,font=('arial', 16,'bold'), textvarible=Burger, bd=10, insertwidth=4,
                  bg="powder blue", justify='right')
txtBurger.grid(row=2,column=1)


lblFilet = Label(f1,font=('arial', 16,'bold'), text="Filet_o_meal", bd=16, anchor='w')
lblFilet.grid(row=3,column=0)
txtFilet=Entry(f1,font=('arial', 16,'bold'), textvarible=Filet, bd=10, insertwidth=4,
                  bg="powder blue", justify='right')
txtFilet.grid(row=3,column=1)



lblChicken = Label(f1,font=('arial', 16,'bold'), text="Chicken Meal", bd=16, anchor='w')
lblChicken.grid(row=4,column=0)
txtChicken = Entry(f1,font=('arial', 16,'bold'), textvarible=Chicken_Burger, bd=10, insertwidth=4,
                  bg="powder blue", justify='right')
txtChicken.grid(row=4,column=1)


lblCheese = Label(f1,font=('arial', 16,'bold'), text="Cheese Meal", bd=16, anchor='w')
lblCheese.grid(row=5,column=0)
txtCheese=Entry(f1,font=('arial', 16,'bold'), textvarible=Cheese_Burger, bd=10, insertwidth=4,
                  bg="powder blue", justify='right')
txtCheese.grid(row=5,column=1)
#==========================================================================================================

#restraunt information 2

lblDrinks = Label(f1,font=('arial', 16,'bold'), text="Drinks", bd=16, anchor='w')
lblDrinks.grid(row=0,column=2)
txtDrinks=Entry(f1,font=('arial', 16,'bold'), textvarible=rand, bd=10, insertwidth=4,
                  bg="#ffffff", justify='right')
txtDrinks.grid(row=0,column=3)


lblCost = Label(f1,font=('arial', 16,'bold'), text="Cost of Meal", bd=16, anchor='w')
lblCost.grid(row=1,column=2)
txtCost=Entry(f1,font=('arial', 16,'bold'), textvarible=Cost, bd=10, insertwidth=4,
                  bg="ffffff", justify='right')
txtCost.grid(row=1,column=3)


lblService = Label(f1,font=('arial', 16,'bold'), text="Service Charge", bd=16, anchor='w')
lblService.grid(row=2,column=2)
txtService = Entry(f1,font=('arial', 16,'bold'), textvarible=Service_Charge, bd=10, insertwidth=4,
                  bg="#ffffff", justify='right')
txtService.grid(row=2,column=3)


lblStateTax = Label(f1,font=('arial', 16,'bold'), text="State Tax", bd=16, anchor='w')
lblStateTax.grid(row=3,column=2)
txtStateTax=Entry(f1,font=('arial', 16,'bold'), textvarible=Tax, bd=10, insertwidth=4,
                  bg="#ffffff", justify='right')
txtStateTax.grid(row=3,column=3)



lblSubTotal = Label(f1,font=('arial', 16,'bold'), text="Sub Total", bd=16, anchor='w')
lblSubTotal.grid(row=4,column=2)
txtSubTotal = Entry(f1,font=('arial', 16,'bold'), textvarible=SubTotal, bd=10, insertwidth=4,
                  bg="#ffffff", justify='right')
txtSubTotal.grid(row=4,column=3)


lblTotalCost = Label(f1,font=('arial', 16,'bold'), text="Total Cost", bd=16, anchor='w')
lblTotalCost.grid(row=5,column=2)
txtTotalCost=Entry(f1,font=('arial', 16,'bold'), textvarible=Total, bd=10, insertwidth=4,
                  bg="#ffffff", justify='right')
txtTotalCost.grid(row=5,column=3)

#======================================Button==========================

btnTotal=Button(f1,padx=16,pady=8, bd=16, fg="Black",font=('arial', 16,'bold'), width=10,
                text="Total", bg="powder blue", command = Ref).grid(row=7, column=1)

btnTotal=Button(f1,padx=16,pady=8, bd=16, fg="Black",font=('arial', 16,'bold'), width=10,
                text="Reset", bg="powder blue", command = Reset).grid(row=7, column=2)

btnExit=Button(f1,padx=16,pady=8, bd=16, fg="Black",font=('arial', 16,'bold'), width=10,
                text="Exit", bg="powder blue", command = qExit).grid(row=7, column=3)

root.mainloop()

当运行这段代码时,我有以下输出

============== RESTART: E:\Gui restraurant managment part 1.py ==============
    Traceback (most recent call last):
      File "E:\Gui restraurant managment part 1.py", line 143, in <module>
        bg="powder blue", justify='right')
      File "C:\Users\simon\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 2516, in __init__
        Widget.__init__(self, master, 'entry', cnf, kw)
      File "C:\Users\simon\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 2139, in __init__
        (widgetName, self._w) + extra + self._options(cnf))
    _tkinter.TclError: unknown option "-textvarible"
    >>> 

我怎样才能让它工作?

【问题讨论】:

这在什么方面不起作用? @SimonRaivid 将您的代码编辑为尽可能短,以重现您的问题,并清楚地说明什么不起作用。 【参考方案1】:

在行中

txtRefernce=Entry(f1,font=('arial', 16,'bold'),    
textvarible=rand, bd=10, 
insertwidth=4,bg="powder blue", justify='right')

您在 textvariable 参数中忘记了 A

txtRefernce=Entry(f1,font=('arial', 16,'bold'), 
textvariable=rand, bd=10, 
insertwidth=4,bg="powder blue", justify='right')

错误在-textvariable 参数中 尝试纠正这部分

第 195 行 中,您忘记了 #ffffff 颜色中的 #

当我在这里更正它时它工作得很好。

【讨论】:

能否请您给我看看您的代码,因为我的代码不起作用 你纠正我说的话了吗?如果是这样,它现在出现的错误消息是什么查看错误消息的内容非常重要。在您的情况下,它说:文件“E:\ Gui餐厅管理部分1.py”,第143行,在 bg="powder blue", justify='right') 然后我进入第 143 行检查程序中的错误,并意识到这只是一个打字错误(可能是复制粘贴)

以上是关于显示意外行为的 tkinter 代码的主要内容,如果未能解决你的问题,请参考以下文章

如何从Python Tkinter应用程序中捕获任何输出到控制台?

MatplotLib (TKinter) + OpenCV 在 Python 3 中崩溃

Tkinter:如何使用线程来防止主事件循环“冻结”

Tkinter 在 Mac 上的 Tk() 上不断崩溃

Python 2.7 Tkinter标签虽然有效但未显示

如何在 tkinter 中显示数据框