如何从下拉列表中选择“是”时显示和隐藏tkinter输入框

时间:2018-06-12 06:49:00

标签: user-interface tkinter python-3.6

我使用tkinter创建了一个供个人使用的参赛表格。现在我想添加一个函数,如果是,如果从下拉显示输入框中选择,否则隐藏它。

我已经编写了代码,但它们都没有工作 抛出以下错误是我的代码片段

field = ["YES",""]
query_text = StringVar()
lblname = Label(f1aa, font=("arial", 10, "bold"), text="Query/Reply", bd=8, anchor="w")
lblname.grid(row=0, column=0, sticky=W)
txtname28 = OptionMenu(f1aa, query_text, *field)
txtname28.grid(row=0, column=1, sticky=W)

hidden = False
hidden_text = StringVar()
e = Entry(f1aa, font=("arial", 10, "bold"), bd=8, justify="left", textvariable = hidden_text)
e.grid(row=0, column=2)

if query_text == "YES":
    e.grid()
else:
    e.grid_remove()
#hidden = not hidden

完整代码

from tkinter import *
from tkinter import ttk
import random
import time
import datetime
from datetime import date
from tkinter import messagebox
import Backend
import csv
import sqlite3
import re

root = Tk()
root.geometry("1350x750+0+0")
root.title("OTRS Management System")
root.configure(background="gray28")

Tops = Frame(root, width=1350, height=100,bd=14, relief="raise")
Tops.pack(side=TOP)

f1 = Frame(root, width=900, height=650, bd=8, relief="raise")
f1.pack(side=LEFT)

f2 = Frame(root, width=440, height=650,bd=8, relief="raise")
f2.pack(side=RIGHT)

ft2 = Frame(f2, width=440, height=450,bd=12, relief="raise")
ft2.pack(side=TOP)
fb2 = Frame(f2, width=440, height=250,bd=16, relief="raise")
fb2.pack(side=BOTTOM)

f1a = Frame(f1, width=900, height=330,bd=8, relief="raise")
f1a.pack(side=TOP)
f2a = Frame(f1, width=900, height=320,bd=6, relief="raise")
f2a.pack(side=BOTTOM)

f1aa = Frame(f1a, width=400, height=330,bd=16, relief="raise")
f1aa.pack(side=LEFT)
f1ab = Frame(f1a, width=400, height=330,bd=16, relief="raise")
f1ab.pack(side=RIGHT)

f2aa = Frame(f2a, width=450, height=330,bd=14, relief="raise")
f2aa.pack(side=LEFT)
f2ab = Frame(f2a, width=450, height=330,bd=14, relief="raise")
f2ab.pack(side=LEFT)

Tops.configure(background="LightGoldenrod4")
f1.configure(background="LightGoldenrod4")
f2.configure(background="LightGoldenrod4")

field = ["YES",""]

def view_command():
    list1.delete(0,END)
    for row in Backend.view():
        list1.insert(END,row)

def add_command():
    count_total_selected = [query_text.get(),
                            phase_and_tower.get(), saleable_mapping.get(), floor_plans.get(), options.get(), images.get(),
                            cons.get(), video.get(), date.get(), amenities.get(), bank.get(), latlong.get(), usp.get(),
                            facttable.get(), prj_name.get(), prj_desp.get(), specification.get(), builder.get(), tco.get(),
                            brochure.get(), prj_deactivation.get(), np.get(), np2.get(), np_refresh.get()]
    count = 0
    for i in count_total_selected:
        if i=="YES":
            count+=1
    print(count)

    if str(ticket_text.get()).isdigit()== False or len(str(ticket_text.get())) < 4  or ticket_text.get()=="" or agent_text.get()=="" or queue_text.get()=="" or inventory.get()=="":
        toplevel = Toplevel()
        label1 = Label(toplevel, text="***If you are receiving this it means you have forgotten the below points: \n Ticket number is not entered \n Queue is not selected \n Agent Name is not selected.\n Please note if XID number is not available write NA in the BOX\n Please make sure XID number starts with r or c.", height=10, width=55)
        label1.pack()
    else:
        Backend.insert(fdate, ticket_text.get(),agent_text.get(), query_text.get(), queue_text.get(), phase_and_tower.get(),saleable_mapping.get(),floor_plans.get(),options.get(), images.get(),cons.get(), video.get(),date.get(),amenities.get(),bank.get(),latlong.get(), usp.get(), facttable.get(),prj_name.get(),prj_desp.get(),specification.get(),builder.get(),tco.get(),brochure.get(),prj_deactivation.get(),np.get(),np2.get(), inventory.get(), np_refresh.get(), count)
        list1.delete(0,END)
        list1.insert(END,(fdate, ticket_text.get(),agent_text.get(), query_text.get(), queue_text.get(), phase_and_tower.get(),saleable_mapping.get(),floor_plans.get(),options.get(), images.get(),cons.get(), video.get(),date.get(),amenities.get(),bank.get(),latlong.get(), usp.get(), facttable.get(),prj_name.get(),prj_desp.get(),specification.get(),builder.get(),tco.get(),brochure.get(),prj_deactivation.get(),np.get(),np2.get(), inventory.get(), np_refresh.get(), count))

def reset():
    phase_and_tower.set("")
    saleable_mapping.set("")
    floor_plans.set("")
    options.set("")
    images.set("")
    video.set("")
    date.set("")
    amenities.set("")
    prj_deactivation.set("")
    inventory.set("")
    np2.set("")
    bank.set("")
    latlong.set("")
    usp.set("")
    facttable.set("")
    prj_name.set("")
    prj_desp.set("")
    specification.set("")
    builder.set("")
    tco.set("")
    np.set("")
    cons.set("")
    brochure.set("")
    agent_text.set("")
    queue_text.set("")
    np_refresh.set("")
    query_text.set("")
    ticket_text.set("")
def download():
    conn = sqlite3.connect("OtrsSummary.db")
    curs = conn.cursor()
    data = curs.execute("select * from otrs")
    m_dict = list(data.fetchall())
    print(m_dict)

    filename = "Productivity.csv"
    with open("./"+filename,'w') as csvfile:
        csvfile = csv.writer(csvfile, m_dict)
    #csvfile.writerow(titleRow)
        csvfile.writerow(["id","Date","TicketNumber","Agent","QueryReply","Queue","PhasesandTowers","SaleableMapping","Floorplan","Options","Images","ConstructionImages","video","PossessionDate","Amenities","Bank","Location","USP","FactTable","ProjectName","Description","Specificaton","Builder","TCO","Brochure","ProjectDeactivation","NPDeactivation","NewBooking","XID Number","np_refresh","Total"])
        for i in range(0, len( m_dict )):
        #print(alldata1[i])
            csvfile.writerow( m_dict[i]  )

localtime = time.asctime(time.localtime(time.time()))
fdate = date.today()
lblInfo = Label(Tops, font=("arial", 30, "bold"), text="OTRS Management System", fg="SteelBlue4", bd=10)
lblInfo.grid(row=0, column=0)
lblInfo = Label(Tops, font=("arial", 30, "bold"), text=localtime, fg="IndianRed1",bd=10)
lblInfo.grid(row=0, column=1)
#===============================All Functions===================
#===========================#first part=======================
phase_and_tower = StringVar()
lblname2 = Label(f1aa, font=("arial", 10, "bold"), text="Phases and Tower", bd=8, anchor="w")
lblname2.grid(row=1, column=0, sticky=W)
txtname1 = OptionMenu(f1aa, phase_and_tower, *field)
txtname1.grid(row=1, column=1, sticky=W)


saleable_mapping = StringVar()
lblname2 = Label(f1aa, font=("arial", 10, "bold"), text="Saleable Mapping", bd=8, anchor="w")
lblname2.grid(row=2, column=0, sticky=W)
txtname2 = OptionMenu(f1aa, saleable_mapping, *field)
txtname2.grid(row=2, column=1, sticky=W)

floor_plans = StringVar()
lblname2 = Label(f1aa, font=("arial", 10, "bold"), text="Floor Plan", bd=8, anchor="w")
lblname2.grid(row=3, column=0, sticky=W)
txtname3 = OptionMenu(f1aa, floor_plans, *field)
txtname3.grid(row=3, column=1, sticky=W)

options = StringVar()
lblname2 = Label(f1aa, font=("arial", 10, "bold"), text="Options", bd=8, anchor="w")
lblname2.grid(row=4, column=0, sticky=W)
txtname4 = OptionMenu(f1aa, options, *field)
txtname4.grid(row=4, column=1, sticky=W)

images = StringVar()
lblname2 = Label(f1aa, font=("arial", 10, "bold"), text="Images", bd=8, anchor="w")
lblname2.grid(row=5, column=0, sticky=W)
txtname5 = OptionMenu(f1aa, images, *field)
txtname5.grid(row=5, column=1, sticky=W)


video = StringVar()
lblname2 = Label(f1aa, font=("arial", 10, "bold"), text="Video", bd=8, anchor="w")
lblname2.grid(row=6, column=0, sticky=W)
txtname6 = OptionMenu(f1aa, video, *field)
txtname6.grid(row=6, column=1, sticky=W)

##rera = StringVar()
##lblname2 = Label(f1aa, font=("arial", 10, "bold"), text="Rera", bd=8, anchor="w")
##lblname2.grid(row=7, column=0, sticky=W)
##txtname7 = Entry(f1aa, font=("arial", 10, "bold"), bd=8, justify="left", textvariable=rera)
##txtname7.grid(row=7, column=1, sticky=W)

date = StringVar()
lblname2 = Label(f1aa, font=("arial", 10, "bold"), text="Possession Status/Date", bd=8, anchor="w")
lblname2.grid(row=8, column=0, sticky=W)
txtname8 = OptionMenu(f1aa, date, *field)
txtname8.grid(row=8, column=1, sticky=W)

amenities = StringVar()
lblname2 = Label(f1aa, font=("arial", 10, "bold"), text="Amenities", bd=8, anchor="w")
lblname2.grid(row=9, column=0, sticky=W)
txtname9 = OptionMenu(f1aa, amenities, *field)
txtname9.grid(row=9, column=1, sticky=W)

prj_deactivation = StringVar()
lblname2 = Label(f1aa, font=("arial", 10, "bold"), text="Project Deactivation", bd=8, anchor="w")
lblname2.grid(row=10, column=0, sticky=W)
txtname10 = OptionMenu(f1aa, prj_deactivation, *field)
txtname10.grid(row=10, column=1, sticky=W)

inventory = StringVar()
lblname2 = Label(f2ab, font=("arial", 10, "bold"), text="XID Number", bd=8, anchor="w")
lblname2.grid(row=1, column=1, sticky=W)
txtname11 = Entry(f2ab, font=("arial", 10, "bold"), bd=8, justify="left", textvariable=inventory)
txtname11.grid(row=1, column=2, sticky=W)

np2 = StringVar()
lblname2 = Label(f1aa, font=("arial", 10, "bold"), text="New Booking/Resale Lock", bd=8, anchor="w")
lblname2.grid(row=12, column=0, sticky=W)
txtname12 = OptionMenu(f1aa, np2, *field)
txtname12.grid(row=12, column=1, sticky=W)


query_text = StringVar()
lblname = Label(f1aa, font=("arial", 10, "bold"), text="Query/Reply", bd=8, anchor="w")
lblname.grid(row=0, column=0, sticky=W)
txtname28 = OptionMenu(f1aa, query_text, *field)
txtname28.grid(row=0, column=1, sticky=W)

hidden = False
hidden_text = StringVar()
e = Entry(f1aa, font=("arial", 10, "bold"), bd=8, justify="left", textvariable = hidden_text)
e.grid(row=0, column=2)

if query_text == "YES":
    e.grid()
else:
    e.grid_remove()
#hidden = not hidden


#######################################part2=====================
bank = StringVar()
lblname2 = Label(f1ab, font=("arial", 10, "bold"), text="Bank", bd=8, anchor="w")
lblname2.grid(row=1, column=0, sticky=W)
txtname13 = OptionMenu(f1ab, bank, *field)
txtname13.grid(row=1, column=1, sticky=W)

latlong = StringVar()
lblname2 = Label(f1ab, font=("arial", 10, "bold"), text="Lat Long/Location", bd=8, anchor="w")
lblname2.grid(row=2, column=0, sticky=W)
txtname14 = OptionMenu(f1ab, latlong, *field)
txtname14.grid(row=2, column=1, sticky=W)

usp = StringVar()
lblname2 = Label(f1ab, font=("arial", 10, "bold"), text="USP", bd=8, anchor="w")
lblname2.grid(row=3, column=0, sticky=W)
txtname15 = OptionMenu(f1ab, usp, *field)
txtname15.grid(row=3, column=1, sticky=W)

facttable = StringVar()
lblname2 = Label(f1ab, font=("arial", 10, "bold"), text="Fact Table", bd=8, anchor="w")
lblname2.grid(row=4, column=0, sticky=W)
txtname16 = OptionMenu(f1ab, facttable, *field)
txtname16.grid(row=4, column=1, sticky=W)

prj_name = StringVar()
lblname2 = Label(f1ab, font=("arial", 10, "bold"), text="Project Name", bd=8, anchor="w")
lblname2.grid(row=5, column=0, sticky=W)
txtname17 = OptionMenu(f1ab, prj_name, *field)
txtname17.grid(row=5, column=1, sticky=W)

prj_desp = StringVar()
lblname2 = Label(f1ab, font=("arial", 10, "bold"), text="Project Description", bd=8, anchor="w")
lblname2.grid(row=6, column=0, sticky=W)
txtname18 = OptionMenu(f1ab, prj_desp, *field)
txtname18.grid(row=6, column=1, sticky=W)

specification = StringVar()
lblname2 = Label(f1ab, font=("arial", 10, "bold"), text="Project Specification", bd=8, anchor="w")
lblname2.grid(row=7, column=0, sticky=W)
txtname19 = OptionMenu(f1ab, specification, *field)
txtname19.grid(row=7, column=1, sticky=W)

builder = StringVar()
lblname2 = Label(f1ab, font=("arial", 10, "bold"), text="Builer Details", bd=8, anchor="w")
lblname2.grid(row=8, column=0, sticky=W)
txtname20 = OptionMenu(f1ab, builder, *field)
txtname20.grid(row=8, column=1, sticky=W)

tco = StringVar()
lblname2 = Label(f1ab, font=("arial", 10, "bold"), text="TCO/Payment Plan", bd=8, anchor="w")
lblname2.grid(row=9, column=0, sticky=W)
txtname21 = OptionMenu(f1ab, tco, *field)
txtname21.grid(row=9, column=1, sticky=W)

np = StringVar()
lblname2 = Label(f1ab, font=("arial", 10, "bold"), text="NP Deactivation", bd=8, anchor="w")
lblname2.grid(row=10, column=0, sticky=W)
txtname22 = OptionMenu(f1ab, np, *field)
txtname22.grid(row=10, column=1, sticky=W)

cons = StringVar()
lblname2 = Label(f1ab, font=("arial", 10, "bold"), text="Construction Images", bd=8, anchor="w")
lblname2.grid(row=11, column=0, sticky=W)
txtname23 = OptionMenu(f1ab, cons, *field)
txtname23.grid(row=11, column=1, sticky=W)

brochure = StringVar()
lblname2 = Label(f1ab, font=("arial", 10, "bold"), text="Brochure", bd=8, anchor="w")
lblname2.grid(row=12, column=0, sticky=W)
txtname24 = OptionMenu(f1ab, brochure, *field)
txtname24.grid(row=12, column=1, sticky=W)


###======EntryWidget for checkboxes=======
#==================for Reset======================
list1=Listbox(ft2, height=22,width=59)
list1.grid(row=2,column=0,rowspan=6,columnspan=2)

sb1=Scrollbar(ft2)
sb1.grid(row=2,column=2,rowspan=6)

list1.configure(yscrollcommand=sb1.set)
sb1.configure(command=list1.yview)

list1.bind('<<ListboxSelect>>')

lblreceipt = Label(ft2, font=("arial", 12, "bold"), text="View Data", bd=2, anchor="w")
lblreceipt.grid(row=1, column=0, sticky=W)

##txtReceipt = Text(ft2, width=59, height=22, bg="white", bd=8, font=("arial", 11, "bold"))
##txtReceipt.grid(row=2, column=0)

##########buttons########################
btnReset = Button(fb2, padx=16,pady=1,bd=4, fg="black", font=("arial", 8, "bold"),width=5,
                   text="Reset", command=reset).grid(row=0, column=1)
btnSubmit = Button(fb2, padx=16, pady=1, bd=4, fg="black", font=("arial", 8, "bold"),width=5,
                   text="Submit", command=add_command).grid(row=0, column=2)
btnExit = Button(fb2, padx=16, pady=1, bd=4, fg="black", font=("arial", 8, "bold"),width=5,
                   text="Exit", command=root.destroy).grid(row=0, column=4)

btnview = Button(fb2, padx=16, pady=1, bd=4, fg="black", font=("arial", 8, "bold"),width=5,
                   text="View Data", command=view_command).grid(row=0, column=3)

download = Button(fb2, padx=16, pady=1, bd=4, fg="black", font=("arial", 8, "bold"),width=5,
                   text="Download Data", command=download).grid(row=1, column=1)

########################################################################################
ticket_text = StringVar()
lblname2 = Label(f2aa, font=("arial", 10, "bold"), text="Ticket Number", bd=8, anchor="w")
lblname2.grid(row=1, column=0, sticky=W)
txtname25 = Entry(f2aa, font=("arial", 10, "bold"), bd=8, justify="left", textvariable=ticket_text)
txtname25.grid(row=1, column=1, sticky=W)

##xid_text = StringVar()
##lblname3 = Label(f2aa, font=("arial", 10, "bold"), text="XID/TID", bd=8, anchor="w")
##lblname3.grid(row=2, column=0, sticky=W)
##txtname26 = Entry(f2aa, font=("arial", 10, "bold"), bd=8, justify="left", textvariable = xid_text)
##txtname26.grid(row=2, column=1, sticky=W)
agents = ["Himani",
"Jhanvi",
"Sandhya",
"Juhi",
"Ruchika",
"Saurabh",
"Neha R",
"Shubhangi",
"Alisha",
"Priya S",
"Khushbu",
"Gaurav",
"Manisha",
"Anjali S",
"Sneha",
"Priyadeep",
"Anjali B",
"Rashi",
"Palak",
"Shivangi",
"Khyati",
"Alok",
"Vikas",
"Tariq",
"Vinay",
"Nupur",
"Mansi"]

agent_text = StringVar()
lblname4 = Label(f2aa, font=("arial", 10, "bold"), text="Agents Name", bd=8, anchor="w")
lblname4.grid(row=3, column=0, sticky=W)
txtname27 = OptionMenu(f2aa, agent_text, *agents)
txtname27.grid(row=3, column=1, sticky=W)

field1 = ["UPGRADES", "CREATION", "MODIFICATION"]
queue_text = StringVar()
lblname2 = Label(f2ab, font=("arial", 10, "bold"), text="Queue", bd=8, anchor="w")
lblname2.grid(row=0, column=1, sticky=W)
txtname29 = OptionMenu(f2ab, queue_text, *field1)
txtname29.grid(row=0, column=2, sticky=W)

np_refresh = StringVar()
lblname31 = Label(f1aa, font=("arial", 10, "bold"), text="NP Slot changes/Refresh", bd=8, anchor="w")
lblname31.grid(row=11, column=0, sticky=W)
txtname31 = OptionMenu(f1aa, np_refresh, *field)
txtname31.grid(row=11, column=1, sticky=W)


##sales_text = StringVar()
##lblname3 = Label(f2ab, font=("arial", 10, "bold"), text="Sales Name", bd=8, anchor="w")
##lblname3.grid(row=2, column=0, sticky=W)
##txtname30 = Entry(f2ab, font=("arial", 10, "bold"), bd=8, justify="left", textvariable = sales_text)
##txtname30.grid(row=2, column=1, sticky=W)

##slot = StringVar()
##lblname2 = Label(f2ab, font=("arial",10,"bold"), text="NP Slot", bd=8, anchor="w")
##lblname2.grid(row=0, column=3, sticky=W)
##txtname31 = Entry(f2ab, font=("arial", 10, "bold"), bd=8, justify="left", textvariable=slot)
##txtname31.grid(row=0, column=4, sticky=W)


##city = StringVar()
##lblname5 = Label(f2ab, font=("arial", 10, "bold"), text="Main City", bd=8, anchor="w")
##lblname5.grid(row=1, column=3, sticky=W)
##txtname32 = Entry(f2ab, font=("arial", 10, "bold"), bd=8, justify="left", textvariable = city)
##txtname32.grid(row=1, column=4, sticky=W)

##comments = StringVar()
##lblname6 = Label(f2ab, font=("arial", 10, "bold"), text="Comments", bd=8, anchor="w")
##lblname6.grid(row=2, column=3, sticky=W)
##txtname33 = Entry(f2ab, font=("arial", 10, "bold"), bd=8, justify="left", textvariable = comments)
##txtname33.grid(row=2, column=4, sticky=W)
##############====connecting db with fromntend====


root.mainloop()

我添加了完整代码

2 个答案:

答案 0 :(得分:2)

每次更改OptionMenu选择时,都必须确保显示或隐藏条目框的逻辑。你可以把它放在一个函数中并使用OptionMenu的command参数:

from tkinter import *

root = Tk()

def hide(choice):
    if choice == "YES":
        e.grid()
    else:
        e.grid_remove()

field = ["YES",""]
query_text = StringVar()
lblname = Label(root, font=("arial", 10, "bold"), text="Query/Reply", bd=8, anchor="w")
lblname.grid(row=0, column=0, sticky=W)
txtname28 = OptionMenu(root, query_text, *field, command=hide)
txtname28.grid(row=0, column=1, sticky=W)

hidden_text = StringVar()
hidden_text.set('Show this text?')
e = Entry(root, font=("arial", 10, "bold"), bd=8, justify="left", textvariable = hidden_text)
e.grid(row=0, column=2)

root.mainloop()

答案 1 :(得分:0)

您要做的是将root.mainloop()替换为您自己创建的自定义循环,并将if语句合并到其中。正如你现在所拥有的那样,这个声明只会被调用一次,并且在GUI开始运行之前就已经开始了。而不是检查值是否在开始时只改变了一次,而是需要让它一直保持检查。

检查事情何时一直发生是root.mainloop()的作用,但你需要的是root.mainloop()以外的事情。所以你需要的是你自己创建的一个循环(正如我上面所说),它可以检查值何时发生变化并相应地更新GUI。

在您的循环中,您还需要确保您的GUI仍在手动响应。这通常由root.mainloop()完成,但由于您需要更换它,您需要自己完成。有很多方法可以做到这一点,但首先要查看root.update()并将其置于自己的循环中。

编辑:实际上,您是否考虑过使用复选框而不是下拉菜单?您可以攻击它的功能,并在按下它时显示或隐藏它。此外,您应该考虑启用和禁用窗口小部件,而不是隐藏或显示它。这意味着布局的问题会减少