Python ttk.combobox:如何在框内放置默认文本?
尝试使用Python ttk.combobox:如何在框内放置默认文本?,python,python-3.x,tkinter,combobox,ttk,Python,Python 3.x,Tkinter,Combobox,Ttk,尝试使用textvariable和insert但无效。我希望做到的是: 但是,当您尚未选择任何内容时,文本不会作为值存储在组合框中,而只是默认文本 这是我目前的代码: root = Tk() root.geometry('800x600') root.title('CSSD') topFrame=Frame(root,width=800,height=150,pady=10,padx=250) area=Label(topFrame,text='CSSD') area.config(font=(
textvariable
和insert
但无效。我希望做到的是:
但是,当您尚未选择任何内容时,文本不会作为值存储在组合框中,而只是默认文本
这是我目前的代码:
root = Tk()
root.geometry('800x600')
root.title('CSSD')
topFrame=Frame(root,width=800,height=150,pady=10,padx=250)
area=Label(topFrame,text='CSSD')
area.config(font=("Courier", 100))
frame=Frame(root,highlightbackground="black", highlightcolor="black", highlightthickness=1, width=100, height=100, bd= 0)
frame.place(relx=.5, rely=.5, anchor="center")
username = Label(frame, text='User Name')
username.config(font='Arial',width=15)
password = Label(frame, text='Password')
password.config(font='Arial',width=15)
enteruser = Entry(frame, textvariable=StringVar(),font=large_font)
enterpass = Entry(frame, show='*', textvariable=StringVar(),font=large_font)
combo=ttk.Combobox(frame)
combo['values']=('Clinic 1','Clinic 2','Clinic 3','Clinic 4','Clinic 5','Clinic 6','Clinic 10','Clinic 11','OB')
combo.state(['readonly'])
combo.grid(row=0,sticky=NW)
topFrame.grid(row=0,sticky=EW)
area.grid(row=0,columnspan=300)
username.grid(row=1, sticky=E)
enteruser.grid(row=1, column=1)
password.grid(row=2, sticky=E)
enterpass.grid(row=2, column=1)
您可以像这样使用
set
方法:
combo.set('default')
这只是另一种选择,以防没人提及
method=StringVar(value='default')
choosen=ttk.Combobox(根,宽度=14,textvariable=method,)
这也在
Checkbutton
中起作用。谢谢,这已经起作用了。但是,当我选择一个值然后取消选择它时,默认文本是否可能重新出现?@lu_-laude它当前设置为“只读”。这会改变吗?不,这没关系。谢谢你的帮助。