Tkiner 编程小示例

hxy    2020-04-15 18:07

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,测试一下发给其他人可以使用。

 
Last Modified: 2020-05-17 17:02
Views: 1.6K

[[total]] comments

Post your comment
  1. [[item.time]]
    [[item.user.username]] [[item.floor]]Floor
  2. Click to load more...
  3. Post your comment