【问题标题】:ttk.combobox: How to place a default text inside the box?ttk.combobox:如何在框内放置默认文本?
【发布时间】:2017-06-23 01:41:20
【问题描述】:

尝试使用textvariableinsert,但没有成功。我希望达到的是这样的:

但是当您还没有选择任何内容时,文本不会作为一个值存储在组合框中,只是一个默认文本。

这是我目前的代码:

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)

【问题讨论】:

    标签: python python-3.x tkinter combobox ttk


    【解决方案1】:

    您可以像这样使用set 方法:

    combo.set('default')
    

    【讨论】:

    • 感谢这工作。但是当我选择一个值然后取消选择它时,默认文本是否可能重新出现?
    • @lu_laude 目前设置为“只读”。这会改变吗?
    • 不,没关系。感谢您的帮助。
    【解决方案2】:

    只是另一种选择,以防没人提及。

    method = StringVar(value='default') 
    choosen = ttk.Combobox(root, width = 14, textvariable = method, )
    

    这也适用于Checkbutton

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-11
      • 1970-01-01
      相关资源
      最近更新 更多