def __init__(self, master): self.master = master master.title("查詢窗口") # 获取屏幕的宽度和高度 screen_width = root.winfo_screenwidth() screen_height = root.winfo_screenheight() # 将窗口的大小设置为屏幕的大小 master.geometry("%dx%d" % (screen_width, screen_height)) # 下载按钮 # 日期标签和输入框 ri = datetime.datetime.now().strftime('%Y/%m/%d') self.date_label = tk.Label(master, text="日期:") self.date_label.grid(row=0, column=0, sticky=tk.W) self.date_entry = tk.Entry(master) self.date_entry.grid(row=0, column=1, sticky=tk.W) self.date_entry.insert(0, ri) self.line_label1 = tk.Label(master, text="线别:") self.line_label1.grid(row=1, column=0, sticky=tk.W) self.line_entry1 = tk.Entry(master) self.line_entry1.grid(row=1, column=1, sticky=tk.W) self.line_label2 = tk.Label(master, text="点检表单:") self.line_label2.grid(row=2, column=0, sticky=tk.W) self.line_entry2 = tk.Entry(master) self.line_entry2.grid(row=2, column=1, sticky=tk.W) # 查询按钮 self.query_button = tk.Button(master, text="查询", command=self.query) self.query_button.grid(row=0, column=2, sticky=tk.W) # 结果标签和文本框 self.result_label = tk.Label(master, text="") self.result_label.grid(row=2, column=0, sticky=tk.W) self.result_text = tk.Text(master) self.result_text.grid(row=3, column=0, columnspan=2, sticky=tk.W) self.result_text.configure(width=120) # 设置字体和背景颜色 self.date_label.config(font=("Arial", 14), bg="white") self.date_entry.config(font=("Arial", 14), bg="white") self.line_label1.config(font=("Arial", 14), bg="white") self.line_entry1.config(font=("Arial", 14), bg="white") self.line_label2.config(font=("Arial", 14), bg="white") self.line_entry2.config(font=("Arial", 14), bg="white") self.query_button.config(font=("Arial", 14), bg="white") self.result_label.config(font=("Arial", 14), bg="white") self.result_text.config(font=("Arial", 14), bg="white")將這個代碼中的輸入框全部改爲下拉輸入框
时间: 2023-12-02 15:02:49 浏览: 110
好的,你需要使用 `tkinter` 中的 `ttk` 模块中提供的 `Combobox` 组件来实现下拉框。具体改动如下:
1. 首先需要在开头引入 `ttk` 模块:`from tkinter import ttk`
2. 然后将所有的 `Entry` 改为 `Combobox`,比如将 `self.date_entry = tk.Entry(master)` 改为 `self.date_entry = ttk.Combobox(master)`
3. 对于下拉框,需要设置可选项,可以使用 `values` 参数,比如 `self.date_entry = ttk.Combobox(master, values=["选项1", "选项2", "选项3"])`
4. 可以设置默认选中项,使用 `current` 参数,比如 `self.date_entry = ttk.Combobox(master, values=["选项1", "选项2", "选项3"], current=0)`
5. 最后需要设置字体和背景颜色,使用 `configure` 方法,比如 `self.date_entry.configure(font=("Arial", 14), background="white")`
修改后的代码如下:
```python
from tkinter import ttk
class QueryWindow:
def __init__(self, master):
self.master = master
master.title("查詢窗口")
# 获取屏幕的宽度和高度
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# 将窗口的大小设置为屏幕的大小
master.geometry("%dx%d" % (screen_width, screen_height))
# 下载按钮
# 日期标签和输入框
ri = datetime.datetime.now().strftime('%Y/%m/%d')
self.date_label = tk.Label(master, text="日期:")
self.date_label.grid(row=0, column=0, sticky=tk.W)
self.date_entry = ttk.Combobox(master, values=["选项1", "选项2", "选项3"])
self.date_entry.grid(row=0, column=1, sticky=tk.W)
self.date_entry.current(0)
self.date_entry.configure(font=("Arial", 14), background="white")
self.line_label1 = tk.Label(master, text="线别:")
self.line_label1.grid(row=1, column=0, sticky=tk.W)
self.line_entry1 = ttk.Combobox(master, values=["选项1", "选项2", "选项3"])
self.line_entry1.grid(row=1, column=1, sticky=tk.W)
self.line_entry1.current(0)
self.line_entry1.configure(font=("Arial", 14), background="white")
self.line_label2 = tk.Label(master, text="点检表单:")
self.line_label2.grid(row=2, column=0, sticky=tk.W)
self.line_entry2 = ttk.Combobox(master, values=["选项1", "选项2", "选项3"])
self.line_entry2.grid(row=2, column=1, sticky=tk.W)
self.line_entry2.current(0)
self.line_entry2.configure(font=("Arial", 14), background="white")
# 查询按钮
self.query_button = tk.Button(master, text="查询", command=self.query)
self.query_button.grid(row=0, column=2, sticky=tk.W)
# 结果标签和文本框
self.result_label = tk.Label(master, text="")
self.result_label.grid(row=2, column=0, sticky=tk.W)
self.result_text = tk.Text(master)
self.result_text.grid(row=3, column=0, columnspan=2, sticky=tk.W)
self.result_text.configure(width=120)
# 设置字体和背景颜色
self.date_label.configure(font=("Arial", 14), background="white")
self.line_label1.configure(font=("Arial", 14), background="white")
self.line_label2.configure(font=("Arial", 14), background="white")
self.query_button.configure(font=("Arial", 14), background="white")
self.result_label.configure(font=("Arial", 14), background="white")
self.result_text.configure(font=("Arial", 14), background="white")
```
阅读全文