class ExcelApp: def init(self, master): self.master = master master.title("Excel App")ri1 = datetime.datetime.now().strftime('%H:%M:%S')self.combobox2 = tk.ttk.Combobox(input_frame, values=[ri1]) self.combobox2.pack(side=tk.LEFT, padx=2, pady=2) self.combobox2.current(0)這個代碼中需要修改獲取的時間需要實時更新的結果輸入在self.combobox2下拉框中
时间: 2024-02-05 09:11:21 浏览: 146
您可以使用`after`方法来实现定时更新时间并更新下拉框中的值。您需要将以下代码添加到`ExcelApp`类中的`__init__`方法中:
```python
# 定义一个函数来更新时间和下拉框的值
def update_time():
# 获取当前时间
current_time = datetime.datetime.now().strftime('%H:%M:%S')
# 更新下拉框的值
self.combobox2['values'] = [current_time]
# 重新设置选中的值
self.combobox2.current(0)
# 每隔1秒钟更新一次时间和下拉框的值
self.master.after(1000, update_time)
# 启动更新时间的函数
update_time()
```
这将在创建`ExcelApp`对象时启动一个函数,该函数将每隔1秒钟更新一次当前时间并更新下拉框中的值。
相关问题
class ExcelApp: def __init__(self, master): self.master = master master.title("Excel App")ri1 = datetime.datetime.now().strftime('%H:%M:%S')self.combobox2 = tk.ttk.Combobox(input_frame, values=[ri1]) self.combobox2.pack(side=tk.LEFT, padx=2, pady=2) self.combobox2.current(0)這個代碼中需要修改獲取的時間需要實時更新
你可以使用 `after()` 方法来定时更新时间。
首先,需要将获取时间的代码封装成一个方法:
```python
def update_time(self):
current_time = datetime.datetime.now().strftime('%H:%M:%S')
self.combobox2.configure(values=[current_time])
self.combobox2.current(0)
```
然后,在 `__init__` 方法中调用这个方法,并使用 `after()` 方法定时更新:
```python
class ExcelApp:
def __init__(self, master):
self.master = master
master.title("Excel App")
# 创建 Combobox
self.combobox2 = tk.ttk.Combobox(input_frame)
self.combobox2.pack(side=tk.LEFT, padx=2, pady=2)
# 更新时间并定时更新
self.update_time()
master.after(1000, self.update_time) # 每隔 1 秒更新一次
def update_time(self):
current_time = datetime.datetime.now().strftime('%H:%M:%S')
self.combobox2.configure(values=[current_time])
self.combobox2.current(0)
```
这样就可以实时更新时间了。
class ExcelApp: def init(self, master): self.master = master master.title("Excel App") # 获取屏幕的宽度和高度 screen_width = master.winfo_screenwidth() screen_height = master.winfo_screenheight() # 将窗口的大小设置为屏幕的大小 master.geometry("%dx%d" % (screen_width, screen_height)) master.state('zoomed')在這個窗口創建一個退出按鈕用來關閉這個窗口的代碼
这段代码缺少创建退出按钮的代码,你可以使用以下代码创建一个退出按钮:
```
exit_button = tk.Button(master, text="Exit", command=master.quit)
exit_button.pack()
```
这将在 Excel 应用程序窗口中创建一个名为 "Exit" 的按钮,并将其与 `master.quit` 方法关联,以便在单击该按钮时关闭窗口。
阅读全文
相关推荐













