values = ["D", "N"] self.label5 = tk.Label(input_frame, text="班別") self.label5.pack(side=tk.LEFT, padx=0.02, pady=0.02) self.combobox5 = tk.ttk.Combobox(input_frame,width=10, values=values) self.combobox5.pack(side=tk.LEFT, padx=0.05, pady=0.05) self.combobox5.current(0)修改这个代码将下拉输入框修改为只能下拉选择,不能手动输入
时间: 2024-03-08 08:49:50 浏览: 95
你可以将`Combobox`的`state`属性设置为`"readonly"`,这样就可以禁止手动输入。修改后的代码如下:
```python
values = ["D", "N"]
self.label5 = tk.Label(input_frame, text="班別")
self.label5.pack(side=tk.LEFT, padx=0.02, pady=0.02)
self.combobox5 = tk.ttk.Combobox(input_frame, width=10, values=values, state="readonly")
self.combobox5.pack(side=tk.LEFT, padx=0.05, pady=0.05)
self.combobox5.current(0)
```
相关问题
self.label2 = tk.Label(input_frame, text="點檢時間") self.label2.pack(side=tk.LEFT, padx=0.02, pady=0.02) self.combobox2 = ttk.Combobox(input_frame) self.combobox2.pack(side=tk.LEFT, padx=0.02, pady=0.02) self.update_time() values = ["唐彪", "汾陽", "劉永剛"] self.label3 = tk.Label(input_frame, text="點檢人") self.label3.pack(side=tk.LEFT, padx=0.02, pady=0.02) self.combobox3 = tk.ttk.Combobox(input_frame, values=values) self.combobox3.pack(side=tk.LEFT, padx=0.02, pady=0.02) self.combobox3.current(0) values = ["D", "N"] self.label5 = tk.Label(input_frame, text="班別") self.label5.pack(side=tk.LEFT, padx=0.02, pady=0.02) self.combobox5 = tk.ttk.Combobox(input_frame, values=values) self.combobox5.pack(side=tk.LEFT, padx=0.05, pady=0.05) self.combobox5.current(0) values = [""] self.label6 = tk.Label(input_frame, text="機種") self.label6.pack(side=tk.LEFT, padx=0.05, pady=0.05) self.combobox6 = tk.ttk.Combobox(input_frame, values=values) self.combobox6.pack(side=tk.LEFT, padx=0.05, pady=0.05) self.combobox6.current(0)將下拉輸入框長度小一點
您可以使用 `width` 参数来设置下拉输入框的长度,例如:
```
self.combobox2 = ttk.Combobox(input_frame, width=10)
```
这将设置下拉输入框的宽度为 10 个字符。您可以根据需要进行调整。
# 创建右侧面板 self.panel_right = tk.Frame(master) self.panel_right.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) # 创建Frame来包含标签和文本框 text_frame = tk.Frame(self.panel_right) text_frame.pack(side=tk.TOP, fill=tk.BOTH, expand=True) # 创建文本框 self.textbox = tk.Text(text_frame) self.textbox.pack(side=tk.TOP, fill=tk.BOTH, expand=True) # 创建标签和下拉框 ri = datetime.datetime.now().strftime('%Y/%m/%d') values = [ri] self.label1 = tk.Label(text_frame, text="點檢日期") self.label1.pack(side=tk.LEFT, padx=5, pady=5) self.combobox1 = tk.ttk.Combobox(text_frame, values=[values]) self.combobox1.pack(side=tk.LEFT, padx=5, pady=5) self.combobox1.current(0) ri1 = datetime.datetime.now().strftime('%H:%M:%S') values3 = [ri1] self.label3 = tk.Label(text_frame, text="點檢人") self.label3.pack(side=tk.LEFT, padx=5, pady=5) self.combobox3 = tk.ttk.Combobox(text_frame, values=values3) self.combobox3.pack(side=tk.LEFT, padx=5, pady=5) self.combobox3.current(0)將標簽和下拉輸入框放到文本框上方
可以将文本框和标签/下拉框分别放到两个 Frame 中,然后将这两个 Frame 放到 self.panel_right 中即可。例如:
```
# 创建右侧面板
self.panel_right = tk.Frame(master)
self.panel_right.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
# 创建Frame来包含标签和下拉框
input_frame = tk.Frame(self.panel_right)
input_frame.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5)
# 创建标签和下拉框
ri = datetime.datetime.now().strftime('%Y/%m/%d')
ri1 = datetime.datetime.now().strftime('%H:%M:%S')
self.label1 = tk.Label(input_frame, text="點檢日期")
self.label1.pack(side=tk.LEFT, padx=5, pady=5)
self.combobox1 = tk.ttk.Combobox(input_frame, values=[ri])
self.combobox1.pack(side=tk.LEFT, padx=5, pady=5)
self.combobox1.current(0)
self.label3 = tk.Label(input_frame, text="點檢人")
self.label3.pack(side=tk.LEFT, padx=5, pady=5)
self.combobox3 = tk.ttk.Combobox(input_frame, values=[ri1])
self.combobox3.pack(side=tk.LEFT, padx=5, pady=5)
self.combobox3.current(0)
# 创建文本框
text_frame = tk.Frame(self.panel_right)
text_frame.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
self.textbox = tk.Text(text_frame)
self.textbox.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
```
这样,标签和下拉框会在一行中显示,位于文本框上方,而且在放大窗口时也会自动跟随变化。
阅读全文