如何根据用户点击的内容实现更改GUI中的值的代码?

时间:2018-03-01 11:50:39

标签: python tkinter

以下程序应该是一个库存管理系统。

from tkinter import*
from tkinter import Tk, StringVar, ttk
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
import random
import datetime
import time;
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
start = Tk()
start.geometry("100x600+0+0")
start.title ("R.E.D Inventory Control System")


#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
Heading = Frame(start, width = 1000, height = 100, bd = 10, relief = 'raise')
Heading.pack(side = TOP)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
LowerHeading = Frame(start, width = 500, height  = 100, bd = 20, relief = 'raise')
LowerHeading.pack(side = BOTTOM)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
LMiddle = Frame(start, width = 500, height = 1000, bd = 12, relief = 'raise')
LMiddle.pack(side = LEFT)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
RMiddle = Frame(start, width = 500, height = 1000, bd = 12, relief = 'raise')
RMiddle.pack(side=RIGHT)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
Title = Label(Heading, font=('arial',40,'bold'), text = "R.E.D Inventory Control System", bd = 10, width = 40, anchor = 'w')
      #justify = 'center')
Title.grid(row=0,column=0)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
info1=IntVar()
info2=StringVar()


info1.set("0")
info2.set("")

当在GUI中选择值ID01时,下面的代码段应该更改GUI的值,以便描述栏显示着装。我不确定为什么会这样。

我将不胜感激。

#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
def Product():
    if (info1.get()=="ID01"):
        info2.set("Dress")


#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
ProductID = Label(LMiddle, font=('arial',12,'bold'),text = "Product ID", bd = 10, width = 15, anchor = 'w')
ProductID.grid(row=0,column=0)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
ProductsID = ttk.Combobox(LMiddle, variable = info1 ,state='readonly', font=('arial',12,'bold'),  width =20)
ProductsID['value']=('','ID01','ID02','ID03','ID04','ID05')
ProductsID.current(0)
ProductsID.grid(row=0,column=1)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
Description1 = Label(LMiddle, font=('arial',12,'bold'),text ="Description", bd = 10, width = 15, anchor = 'w')
Description1.grid(row=2,column=0)
Description2 = Label(LMiddle, font=('arial',12,'bold'), textvariable = info2, bd = 10, width = 18, relief = 'sunken')
Description2.grid(row=2,column=1)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
DateStockBought1 = Label(LMiddle, font=('arial',12,'bold'), text ="Date Stock Bought", bd = 10, width = 15, anchor = 'w')
DateStockBought1.grid(row=3,column=0)
DateStockBought2 = Label(LMiddle, font=('arial',12,'bold'), bd = 10, width = 18, relief = 'sunken')
DateStockBought2.grid(row=3,column=1)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
Price1 = Label(LMiddle, font=('arial',12,'bold'), text ="Price", bd = 10, width = 15, anchor = 'w')
Price1.grid(row=4,column=0)
Price2 = Label(LMiddle, font=('arial',12,'bold'), bd = 10, width = 18, relief = 'sunken')
Price2.grid(row=4,column=1)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
AmountLeft1 = Label(RMiddle, font=('arial',12,'bold'), text ="Amount Left", bd = 10, width = 15, anchor = 'w')
AmountLeft1.grid(row=1,column=0)
AmountLeft2 = Label(RMiddle, font=('arial',12,'bold'), bd = 10, width = 18,     relief = 'sunken')
AmountLeft2.grid(row=1,column=1)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
CurrentSeason1 = Label(RMiddle, font=('arial',12,'bold'), text ="CurrentSeason", bd = 10, width = 15, anchor = 'w')
CurrentSeason1.grid(row=2,column=0)
CurrentSeason2 = Label(RMiddle, font=('arial',12,'bold'), text = "", bd = 10, width = 18, relief = 'sunken')
CurrentSeason2.grid(row=2,column=1)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
Discount1 = Label(RMiddle, font=('arial',12,'bold'), text ="Discount", bd = 10, width = 15, anchor = 'w')
Discount1.grid(row=3,column=0)
Discount2 = Label(RMiddle, font=('arial',12,'bold'), bd = 10, width = 18, relief = 'sunken')
Discount2.grid(row=3,column=1)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
NewPrice1 = Label(RMiddle, font=('arial',12,'bold'), text ="New Price", bd = 10, width = 15, anchor = 'w')
NewPrice1.grid(row=4,column=0)
NewPrice2 = Label(RMiddle, font=('arial',12,'bold'), bd = 10, width = 18, relief = 'sunken')
NewPrice2.grid(row=4,column=1)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#

start.mainloop()

我无法在GUI中更改说明。

如果我添加以下代码,为什么它不适用于计算部分?

def Product(event):
    if (info1.get()=="ID01"):
        info2.set("Dress")
        info3.set("03/08/2016")
        info4.set("£8")
        info5.set("74")
        info6.set("Spring")
        if (info7.get()=="10%"):
                Calculation=info4/0.1
                info8.set(Calculation)

1 个答案:

答案 0 :(得分:0)

工作代码

from tkinter import*
from tkinter import Tk, StringVar, ttk
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
import random
import datetime
import time;
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
start = Tk()
start.geometry("100x600+0+0")
start.title ("R.E.D Inventory Control System")


#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
Heading = Frame(start, width = 1000, height = 100, bd = 10, relief = 'raise')
Heading.pack(side = TOP)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
LowerHeading = Frame(start, width = 500, height  = 100, bd = 20, relief = 'raise')
LowerHeading.pack(side = BOTTOM)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
LMiddle = Frame(start, width = 500, height = 1000, bd = 12, relief = 'raise')
LMiddle.pack(side = LEFT)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
RMiddle = Frame(start, width = 500, height = 1000, bd = 12, relief = 'raise')
RMiddle.pack(side=RIGHT)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
Title = Label(Heading, font=('arial',40,'bold'), text = "R.E.D Inventory Control System", bd = 10, width = 40, anchor = 'w')
      #justify = 'center')
Title.grid(row=0,column=0)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
info1=StringVar()
info2=StringVar()


info1.set("0")
info2.set("")

def Product(event):
    if (info1.get()=="ID01"):
        info2.set("Dress")


#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
ProductID = Label(LMiddle, font=('arial',12,'bold'),text = "Product ID", bd = 10, width = 15, anchor = 'w')
ProductID.grid(row=0,column=0)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
ProductsID = ttk.Combobox(LMiddle, textvariable = info1 ,state='readonly', font=('arial',12,'bold'),  width =20)
ProductsID['value']=('','ID01','ID02','ID03','ID04','ID05')
ProductsID.current(0)
ProductsID.grid(row=0,column=1)
ProductsID.bind("<<ComboboxSelected>>", Product)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
Description1 = Label(LMiddle, font=('arial',12,'bold'),text ="Description", bd = 10, width = 15, anchor = 'w')
Description1.grid(row=2,column=0)
Description2 = Label(LMiddle, font=('arial',12,'bold'), textvariable = info2, bd = 10, width = 18, relief = 'sunken')
Description2.grid(row=2,column=1)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
DateStockBought1 = Label(LMiddle, font=('arial',12,'bold'), text ="Date Stock Bought", bd = 10, width = 15, anchor = 'w')
DateStockBought1.grid(row=3,column=0)
DateStockBought2 = Label(LMiddle, font=('arial',12,'bold'), bd = 10, width = 18, relief = 'sunken')
DateStockBought2.grid(row=3,column=1)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
Price1 = Label(LMiddle, font=('arial',12,'bold'), text ="Price", bd = 10, width = 15, anchor = 'w')
Price1.grid(row=4,column=0)
Price2 = Label(LMiddle, font=('arial',12,'bold'), bd = 10, width = 18, relief = 'sunken')
Price2.grid(row=4,column=1)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
AmountLeft1 = Label(RMiddle, font=('arial',12,'bold'), text ="Amount Left", bd = 10, width = 15, anchor = 'w')
AmountLeft1.grid(row=1,column=0)
AmountLeft2 = Label(RMiddle, font=('arial',12,'bold'), bd = 10, width = 18,     relief = 'sunken')
AmountLeft2.grid(row=1,column=1)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
CurrentSeason1 = Label(RMiddle, font=('arial',12,'bold'), text ="CurrentSeason", bd = 10, width = 15, anchor = 'w')
CurrentSeason1.grid(row=2,column=0)
CurrentSeason2 = Label(RMiddle, font=('arial',12,'bold'), text = "", bd = 10, width = 18, relief = 'sunken')
CurrentSeason2.grid(row=2,column=1)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
Discount1 = Label(RMiddle, font=('arial',12,'bold'), text ="Discount", bd = 10, width = 15, anchor = 'w')
Discount1.grid(row=3,column=0)
Discount2 = Label(RMiddle, font=('arial',12,'bold'), bd = 10, width = 18, relief = 'sunken')
Discount2.grid(row=3,column=1)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#
NewPrice1 = Label(RMiddle, font=('arial',12,'bold'), text ="New Price", bd = 10, width = 15, anchor = 'w')
NewPrice1.grid(row=4,column=0)
NewPrice2 = Label(RMiddle, font=('arial',12,'bold'), bd = 10, width = 18, relief = 'sunken')
NewPrice2.grid(row=4,column=1)
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------#

start.mainloop()

您需要将组合框绑定到函数

ProductsID.bind("<<ComboboxSelected>>", Product)

并将函数参数更改为event

def Product(event):