免责声明:网站内容仅供个人学习记录,禁做商业用途,转载请注明出处。

版权所有 © 2017-2020 NEUSNCP个人学习笔记 辽ICP备17017855号-2

Tkiner 编程小示例

hxy    2020年4月15日 18:07:44

from tkinter import *
class ShowExampleGUI:
    def __init__(self, parent):
        self.label1 = Label(parent, bg = "black", fg = "white",
                            padx = 30, pady = 10, text = "SHOW ME",
                            font=("Times", "24", "bold italic"))
        self.label1.pack()

    def change_text(self, event, newText="HIDE ME"):
        label_text = self.label1.cget("text")
        if label_text == "SHOW ME":
            self.label1.configure(text="HIDE ME")
        else:
            self.label1.configure(text="SHOW ME")
        # self.label1.configure(text=newText)

    def sys_out(self, event):
        from tkinter import messagebox
        if messagebox.askokcancel('Exit','Confirm to exit?'):
            root.destroy()

#main
root = Tk()
root.title("Configure example")
root.geometry("300x300")


show_label=ShowExampleGUI(root)

# 点击鼠标左键切换
root.bind("<ButtonRelease-1>", show_label.change_text)

# 按ESC退出提示
root.bind('<Escape>',show_label.sys_out)
# show_label.change_text("Hide Me")

root.mainloop()
默认标签显示
 
点击切换标签
 
按ESC退出提示

将Python打包成exe文件则需要pyInstaller ,使用如下命令安装:
pip install pyinstaller
安装完之后,使用打包命令:
pyinstaller -F  -w tk.py
打包过程很快,生成tk.exe,测试一下发给其他人可以使用。

 
最近更新: 2020年5月17日 17:02:36
浏览: 1.7K

[[total]] 条评论

添加评论
  1. [[item.time]]
    [[item.user.username]] [[item.floor]]楼
  2. 点击加载更多……
  3. 添加评论