-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
70 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import tkinter | ||
import tkinter.messagebox | ||
|
||
|
||
def main(): | ||
flag = True | ||
|
||
# 修改标签上的文字 | ||
def change_label_text(): | ||
nonlocal flag | ||
flag = not flag | ||
color, msg = ('red', 'Hello, world!')\ | ||
if flag else ('blue', 'Goodbye, world!') | ||
label.config(text=msg, fg=color) | ||
|
||
# 确认退出 | ||
def confirm_to_quit(): | ||
if tkinter.messagebox.askokcancel('温馨提示', '确定要退出吗?'): | ||
top.quit() | ||
|
||
# 创建顶层窗口 | ||
top = tkinter.Tk() | ||
# 设置窗口大小 | ||
top.geometry('240x160') | ||
# 设置窗口标题 | ||
top.title('小游戏') | ||
# 创建标签对象并添加到顶层窗口 | ||
label = tkinter.Label(top, text='Hello, world!', font='Arial -32', fg='red') | ||
label.pack(expand=1) | ||
# 创建一个装按钮的容器 | ||
panel = tkinter.Frame(top) | ||
# 创建按钮对象 指定添加到哪个容器中 通过command参数绑定事件回调函数 | ||
button1 = tkinter.Button(panel, text='修改', command=change_label_text) | ||
button1.pack(side='left') | ||
button2 = tkinter.Button(panel, text='退出', command=confirm_to_quit) | ||
button2.pack(side='right') | ||
panel.pack(side='bottom') | ||
# 开启主事件循环 | ||
tkinter.mainloop() | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters