Python3 tkinter基础 Button command 单击按钮 在console中打印文本

239 阅读1分钟
  •        Python : 3.8.11
  •          OS : Ubuntu Kylin 20.04
  •       Conda : 4.10.1
  •     PyCharm : 2021.1.3 (Community Edition)

代码

"""
@Author : 行初心
@Date   : 8/6/21
"""

import tkinter as tk


# 面向对象的编程思想
class APP:
    def __init__(self, master):
        # 自动选定大小与位置
        self.frame = tk.Frame(master)
        self.frame.pack()

        # 新建一个按钮
        # 自动设置按钮的位置
        self.hi_there = tk.Button(self.frame,
                                  text="打招呼",
                                  command=self._introduce)
        self.hi_there.pack()

    def _introduce(self):
        print("安石榴")


def main():
    root = tk.Tk()
    app = APP(root)

    root.mainloop()


if __name__ == '__main__':
    main()

运行结果

button_command.png

参考资料

学习推荐


Python具有开源、跨平台、解释型和交互式等特性,值得学习。
Python的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。
GUI可以选择PyQt5、PySide2、wxPython、PyGObject、wxWidgets等进行创作。
代码的书写要遵守规范,这样有助于沟通和理解。
每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。