广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python3 GUI
  • 802
分享到

python3 GUI

GUI 2023-01-31 06:01:11 802人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

用python3创建窗口并显示 修改窗口的名字 在窗口中加入标签 在窗口中加入按钮 使按钮有实际意义 添加可编辑文本框 用Tkinter实现一个简单的GUI程序,单击click按钮时会在终端打印出’hello world’

  • python3创建窗口并显示
  • 修改窗口的名字
  • 在窗口中加入标签
  • 在窗口中加入按钮
  • 使按钮有实际意义
  • 添加可编辑文本框
  • 用Tkinter实现一个简单的GUI程序,单击click按钮时会在终端打印出’hello world’,方法一
  • 用Tkinter实现一个简单的GUI程序,单击click按钮时会在终端打印出’hello world’,方法二
  • 添加2个按钮并改变按钮字体颜色
  • 创建顶级菜单及下拉菜单
  • 按钮相应显示在窗口而非终端
  • 改变标签背景色
  • pack常用属性
  • 页面布局1
  • 页面布局2
#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import tkinter

top=tkinter.Tk() 

#进入消息循环体
top.mainloop() 

这里写图片描述

#!/usr/bin/Python3
# -*- coding: UTF-8 -*-

import tkinter

top=tkinter.Tk(className='hello world') 

#进入消息循环体
top.mainloop()

这里写图片描述

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import tkinter

top=tkinter.Tk(className='hello world') 

#加上标签
label = tkinter.Label(top)  
label['text'] = 'be on your own'  
label.pack() 

#进入消息循环体
top.mainloop()

这里写图片描述

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import tkinter

top=tkinter.Tk(className='hello world') 

#加上标签
label = tkinter.Label(top)  
label['text'] = 'be on your own'  
label.pack() 

#加上按钮
button = tkinter.Button(top)
button['text'] = 'Ok'  
button.pack()

#进入消息循环体
top.mainloop() 

这里写图片描述

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import tkinter

def on_click():  
    label['text'] = 'no way out'

top=tkinter.Tk(className='hello world') 

#加上标签
label = tkinter.Label(top)  
label['text'] = 'be on your own'  
label.pack() 

#加上按钮
button = tkinter.Button(top)
button['text'] = 'Ok'

#添加按钮操作 
button['command'] = on_click 
button.pack()

#进入消息循环体
top.mainloop()

这里写图片描述

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

import tkinter

def on_click():  
    label['text'] = 'no way out'

top=tkinter.Tk(className='hello world') 

#加上标签
label = tkinter.Label(top)  
label['text'] = 'be on your own'  
label.pack() 

#加上按钮
button = tkinter.Button(top)
button['text'] = 'Ok'
#添加按钮操作 
button['command'] = on_click 
button.pack()

#添加可编辑文本框
text = tkinter.StringVar()  
text.set('change to what?')  
entry = tkinter.Entry(top) 
entry['textvariable'] = text  
entry.pack() 

#进入消息循环体
top.mainloop()

这里写图片描述

# #!/usr/bin/python3
# #-*-coding: UTF-8 -*-

import tkinter

def on_click():
    print('hello world')

top=tkinter.Tk(className='hello world') 
#定义窗体的大小,是400X200像素 
top.geometry('400x200')
#加上按钮
button = tkinter.Button(top)
button['text'] = 'click'
#添加按钮操作 
button['command'] = on_click 
button.pack()

#进入消息循环体
top.mainloop()

这里写图片描述

#!/usr/bin/python3
#-*-coding: UTF-8 -*-
from tkinter import *   #引入Tkinter工具包  
def hello():  
    print('hello world!')  

win = Tk()  #定义一个窗体  
win.title('Hello World')    #定义窗体标题  
win.geometry('400x200')     #定义窗体的大小,是400X200像素  

btn = Button(win, text='Click me', command=hello)  
#注意这个地方,不要写成hello(),如果是hello()的话,  
#会在mainloop中调用hello函数,  
# 而不是单击button按钮时出发事件  
btn.pack(expand=YES, fill=BOTH) #将按钮pack,充满整个窗体(只有pack的组件实例才能显示)  

mainloop() #进入主循环,程序运行 

这里写图片描述

#!/usr/bin/python3
#-*-coding: UTF-8 -*-

import tkinter

class App:  
    def __init__(self, master):  
        #构造函数里传入一个父组件(master),创建一个Frame组件并显示  
        frame = tkinter.Frame(master)  
        frame.pack()  
        #创建两个button,并作为frame的一部分  
        self.button = tkinter.Button(frame, text="QUIT", fg="red", command=frame.quit)  
        self.button.pack(side=tkinter.LEFT) #此处side为LEFT表示将其放置 到frame剩余空间的最左方  
        self.hi_there = tkinter.Button(frame, text="Hello", command=self.say_hi)  
        self.hi_there.pack(side=tkinter.LEFT)  

    def say_hi(self):  
        print ("hi there, this is a class example!"  )

win = tkinter.Tk()  
app = App(win)  
win.mainloop() 

这里写图片描述

#!/usr/bin/python3
#-*- encoding=UTF-8 -*-   
import tkinter as tk
root = tk.Tk()  

def hello():  
    print('hello')  

def about():  
    print('我是开发者')  

menubar = tk.Menu(root)  

#创建下拉菜单File,然后将其加入到顶级的菜单栏中  
filemenu = tk.Menu(menubar,tearoff=0)  
filemenu.add_command(label="Open", command=hello)  
filemenu.add_command(label="Save", command=hello)  
filemenu.add_separator()  
filemenu.add_command(label="Exit", command=root.quit)  
menubar.add_cascade(label="File", menu=filemenu)  

#创建另一个下拉菜单Edit  
editmenu = tk.Menu(menubar, tearoff=0)  
editmenu.add_command(label="Cut", command=hello)  
editmenu.add_command(label="Copy", command=hello)  
editmenu.add_command(label="Paste", command=hello)  
menubar.add_cascade(label="Edit",menu=editmenu)  
#创建下拉菜单Help  
helpmenu = tk.Menu(menubar, tearoff=0)  
helpmenu.add_command(label="About", command=about)  
menubar.add_cascade(label="Help", menu=helpmenu)  

#显示菜单  
root.config(menu=menubar)  

tk.mainloop()  

这里写图片描述

#!/usr/bin/python3
#-*- encoding=UTF-8 -*-   
import tkinter as tk
root = tk.Tk()  

def hello():  
    print('hello')  

# def about():  
#     print('我是开发者')  
def about():
    w = tk.Label(root,text="开发者感谢名单\n\nfuyunbiyi\n\nfyby尚未出现的女朋友\n\nHttp://www.programup.com网站")
    w.pack(side=tk.TOP)  

menubar = tk.Menu(root)  

#创建下拉菜单File,然后将其加入到顶级的菜单栏中  
filemenu = tk.Menu(menubar,tearoff=0)  
filemenu.add_command(label="Open", command=hello)  
filemenu.add_command(label="Save", command=hello)  
filemenu.add_separator()  
filemenu.add_command(label="Exit", command=root.quit)  
menubar.add_cascade(label="File", menu=filemenu)  

#创建另一个下拉菜单Edit  
editmenu = tk.Menu(menubar, tearoff=0)  
editmenu.add_command(label="Cut", command=hello)  
editmenu.add_command(label="Copy", command=hello)  
editmenu.add_command(label="Paste", command=hello)  
menubar.add_cascade(label="Edit",menu=editmenu)  
#创建下拉菜单Help  
helpmenu = tk.Menu(menubar, tearoff=0)  
helpmenu.add_command(label="About", command=about)  
menubar.add_cascade(label="Help", menu=helpmenu)  

#显示菜单  
root.config(menu=menubar)  

tk.mainloop()  

这里写图片描述

#!/usr/bin/python3
#-*- encoding=UTF-8 -*-   
import tkinter as tk
root = tk.Tk() 

#创建三个 Label 分别添加到root窗体中 
#Label是一种用来显示文字或者图片的组件
tk.Label(root,text = 'pack1',bg = 'red').pack() 
tk.Label(root, text = 'pack2', bg = 'blue').pack() 
tk.Label(root, text = 'pack3', bg = 'green').pack() 

root.mainloop()

这里写图片描述

属性名 属性简析 取值 取值说明
fill 设置组件是否向水平或垂直方向填充 X、Y、BOTH 和NONE fill = X(水平方向填充)fill = Y(垂直方向填充)fill = BOTH(水平和垂直)NONE 不填充
expand 设置组件是否展开,当值为YES时,side选项无效。组件显示在父容器中心位置;若fill选项为BOTH,则填充父组件的剩余空间。默认为不展开 YES 、NO(1、0) expand=YES expand=NO
side 设置组件的对齐方式 LEFT、TOP、RIGHT、BOTTOM 值为左、上、右、下
ipadx、ipady 设置x方向(或者y方向)内部间隙(子组件之间的间隔) 可设置数值,默认是0 非负整数,单位为像素
padx、pady 设置x方向(或者y方向)外部间隙(与之并列的组件之间的间隔) 可设置数值,默认是0 非负整数,单位为像素
anchor 锚选项,当可用空间大于所需求的尺寸时,决定组件被放置于容器的何处 N、E、S、W、NW、NE、SW、SE、CENTER(默认值为CENTER) 表示八个方向以及中心
from tkinter import *    #注意模块导入方式,否则代码会有差别

class App:
    def __init__(self, master):
        #使用Frame增加一层容器
        fm1 = Frame(master)
        #Button是一种按钮组件,与Label类似,只是多出了响应点击的功能
        Button(fm1, text='Top').pack(side=TOP, anchor=W, fill=X, expand=YES)
        Button(fm1, text='Center').pack(side=TOP, anchor=W, fill=X, expand=YES)
        Button(fm1, text='Bottom').pack(side=TOP, anchor=W, fill=X, expand=YES)
        fm1.pack(side=LEFT, fill=BOTH, expand=YES)

        fm2 = Frame(master)
        Button(fm2, text='Left').pack(side=LEFT)
        Button(fm2, text='This is the Center button').pack(side=LEFT)
        Button(fm2, text='Right').pack(side=LEFT)        
        fm2.pack(side=LEFT, padx=10)


root = Tk()
root.title("Pack - Example")
display = App(root)
root.mainloop()

这里写图片描述

import tkinter as tk

root = tk.Tk()

label1 = tk.Label(root, text = "Height:")
label1.grid(row = 0, column = 0)
label2 = tk.Label(root, text = "Width:")
label2.grid(row = 1, column = 0)

entry1 = tk.Entry(root)
entry1.grid(row = 0, column = 1)
entry2 = tk.Entry(root)
entry2.grid(row = 1, column = 1)

checkbutton = tk.Checkbutton(root, text = "Preserve aspect")
checkbutton.grid(row = 2, column = 0, rowspan = 1, columnspan = 2, sticky=tk.W)#sticky设置控件的对其方位,这里设置为靠西(左西右东)

img = tk.PhotoImage(file = "11.png")
imageview = tk.Label(root, image= img)
imageview.grid(row = 0, column = 2, rowspan = 2, columnspan = 2)

button1 = tk.Button(root, text = "Zoom in")
button1.grid(row = 2, column = 2)
button1 = tk.Button(root, text = "Zoom out")
button1.grid(row = 2, column = 3)

root.mainloop()

这里写图片描述

参考一:https://www.cnblogs.com/monsteryang/p/6558904.html
参考二:https://blog.csdn.net/yingshukun/article/details/53983812
参考三:https://blog.csdn.net/C_Creator/article/details/52383334

--结束END--

本文标题: python3 GUI

本文链接: https://www.lsjlt.com/news/190290.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python3 GUI
    用python3创建窗口并显示 修改窗口的名字 在窗口中加入标签 在窗口中加入按钮 使按钮有实际意义 添加可编辑文本框 用Tkinter实现一个简单的GUI程序,单击click按钮时会在终端打印出’hello world’...
    99+
    2023-01-31
    GUI
  • 基于Python3编写一个GUI翻译器
    目录1、引言2、代码实战2.1 思路2.2 实战3、总结1、引言 小屌丝:鱼哥,你说百度翻译的准确,还是google翻译的准确? 小鱼:自己翻译的最准确。 小屌丝:你这&hellip...
    99+
    2022-11-11
  • 基于Python3制作一个带GUI界面的小说爬虫工具
    目录效果图开发完成后的界面采集过程界面采集后存储主要功能用到的第三方模块打包为 exe 命令全部源码效果图 最近帮朋友写个简单爬虫,顺便整理了下,搞成了一个带GUI界面的小说爬虫工具...
    99+
    2022-11-13
  • 如何使用Python3制作一个带GUI界面的小说爬虫工具
    这篇文章主要介绍如何使用Python3制作一个带GUI界面的小说爬虫工具,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!效果图最近帮朋友写个简单爬虫,顺便整理了下,搞成了一个带GUI界面的小说爬虫工具,用来从笔趣阁爬取...
    99+
    2023-06-29
  • Python Tkinter GUI
    一,Tkinter介绍Tkinter是一个python模块,是一个调用Tcl/Tk的接口,它是一个跨平台的脚本图形界面接口。Tkinter不是唯一的python图形编程接口,但是是其中比较流行的一个。最大的特点是跨平台,缺点是性能不太好,执...
    99+
    2023-01-31
    Python Tkinter GUI
  • JAVA中的GUI
    目录  一.GUI的概念 1.1基本概念 1.2GUI的特点 1.3Swing的概念 1.4GUI中的容器组件 二.常用容器 2.1JFrame 2.2JFrame中常用的方法  2.3JPanel 三.GUI面板的布局 3.1流式布局 ...
    99+
    2023-09-20
    java 开发语言
  • Python GUI 03----But
    1.一个简单的Button应用 from tkinter import * #定义Button的回调函数 def helloButton(): print ('hello button') root = Tk() #通过comma...
    99+
    2023-01-31
    Python GUI
  • Python GUI 之 Combobo
        本章介绍tkinter.ttk的Combobox控件。 2. 环境信息     ********************************     本系列运行平台:Windows10 64bit     Python 版本...
    99+
    2023-01-31
    Python GUI Combobo
  • Python GUI(Tkinter)初
    Python version: 3.7.0效果:代码:import tkinter as tk def openPath():     print("aaa") frm = tk.Tk() frm.title('Auto Rename ...
    99+
    2023-01-31
    Python GUI Tkinter
  • gui design studio3 中
    用户界面 下面就是 GUI Design Studio 工作区。向下滚动滚动条看详细的说明。   多文档 你可以在同一时间里打开和编辑多个设计文档,并可以通过如下方式,轻松的切换:使用工具条下的文档标签;使用Ctrl+Tab激活队列中的下...
    99+
    2023-01-31
    gui design
  • Python GUI 07----Lis
    Listbox为列表框控件,它可以包含一个或多个文本项(text item),可以设置为单选或多选 1.创建一个Listbox,向其中添加三个item from tkinter import * root = Tk() lb = Li...
    99+
    2023-01-31
    GUI Python Lis
  • python--GUI编程--Tkinter
    Tkinter: Tkinter 模块(Tk 接口)是 Python 的标准 Tk GUI 工具包的接口 .Tk 和 Tkinter 可以在大多数的 Unix 平台下使用,同样可以应用在 Windows 和 Macintosh 系统里。Tk...
    99+
    2023-01-30
    python GUI Tkinter
  • python--GUI编程--Tkinter2
    编写一个st2.py文件,代码如下#!/usr/bin/env python # coding: utf-8 __author__ = 'www.py3study.com' ...
    99+
    2023-01-30
    python GUI
  • Python-GUI编程-PyQt5
    Python-GUI编程-PyQt51. GUI编程是什么GUI 全称为: Graphical User Interface;简称GUI翻译为中文为: 图形化用户接口简单理解就是:- 使用Python开发出一个软件的界面,- 让用户可以通过...
    99+
    2023-01-30
    Python GUI
  • 13个Python GUI库
    Python是一门高级编程语言。它用于通用编程。Python语言由Guido van Rossum创建,并于1991年首次发布。Python的设计哲学着重于代码的可读性。因此空白在Python中具有重要的意义。Python提供了允许在小...
    99+
    2023-01-31
    Python GUI
  • 让所有GUI都自动化-PyAutoGUI(GUI自动化工具)
    目录 1、前言 2、简介 3、安装 4、常用函数 5、保护措施 6、鼠标函数 7、键盘函数 8、消息弹窗函数 9、截屏函数 ‍10、示例 1、前言 在使用 Selenium 进行自动化测试时,鼠标事件可以用 ActionChains 类...
    99+
    2023-09-04
    自动化 python 运维
  • Python3教程——5、Python3
            一、Pycharm常用快捷键 有颜色的为很常用,或不易发现   编辑类: Ctrl + Space 基本的代码完成(类、方法、属性) Ctrl + Alt + Space 类名完成 Ctrl + Shift + Ent...
    99+
    2023-01-31
    教程
  • Python GUI编程详解
    目录Python GUI编程0.创建窗口1.Label和Button的使用2.Entry和Text的使用3.Grid网格布局4.Frame框架5.messagebox消息框6.下拉框...
    99+
    2022-11-12
  • python GUI实例学习
    在学习本篇之前,如果你对Python下进行GUI编程基础内容还有不明白,推荐一篇相关文章:简单介绍利用TK在Python下进行GUI编程的教程 写一个简单的界面很容易,即使是什么都不了解的情况下,这个文本转...
    99+
    2022-06-04
    实例 python GUI
  • Python GUI之tkinter详解
    展示 import tkinter if __name__ == '__main__': win = tkinter.Tk() #设置标题 win...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作