python GUI 实例

本文通过两个具体示例介绍了如何使用Python的Tkinter库来创建简单的GUI应用程序。第一个示例展示了如何创建带有按钮的窗口,并在点击按钮时输出特定文本。第二个示例则进一步讲解了如何实现一个可以输入文本并显示输入内容的简单程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/usr/bin/env python3
# coding=utf-8

import tkinter as tk

class Application(tk.Frame):
    def __init__(self,master=None):
        tk.Frame.__init__(self,master)
        self.pack()
        self.createWidgets()
    def createWidgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "hello python\n(click me)"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side="top")
        self.QUIT = tk.Button(self,text="QUIT",fg="red",
                              command=root.destroy)
        self.QUIT.pack(side="bottom")
    def say_hi(self):
        print("hey,look me here!")

root = tk.Tk()
app = Application(master=root)
app.mainloop()


#!/usr/bin/env python3
# coding=utf-8

import tkinter as tk

class Application(tk.Frame):
    def __init__(self,master=None):
        tk.Frame.__init__(self,master)
        self.pack()

        self.entrythingy = tk.Entry()
        self.entrythingy.pack()

        self.contents = tk.StringVar()
        self.contents.set("设置属性")

        self.entrythingy["textvariable"] = self.contents

        self.entrythingy.bind("<Key-Return>",self.print_contents)

    def print_contents(self,event):
        print("当前输入框内容为",self.contents.get())
        
root = tk.Tk()
app = Application(master=root)
app.mainloop()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值