zoukankan      html  css  js  c++  java
  • python-GUI之tkinter的学习

    最近看了哔哩哔哩的python的学习,直接看代码吧,以后会更新

    先来个基础的

    import tkinter as tk #导入包
    
    app = tk.Tk() #抽象出一个GUI
    app.title("first GUI") #设置这个窗口的标题
    thelable = tk.Label(app, text = "这是一个窗口") #标签,要先把参数app给传进去,比较常用
    thelable.pack()  #这是对这个标签进行排版,可以在里面设置参数自己设置
    
    app.mainloop()#必须要加

    加了个按钮,点击按钮就可以出现hello

    import tkinter as tk
    
    class App:
        def __init__(self, master):#构造函数,对App进行初始化
            frame = tk.Frame(master)#按钮框架
            frame.pack(side = tk.LEFT,padx = 100,pady = 100)#设置参数自定义
    
            self.hi_here = tk.Button(frame, text = 'hello',fg='blue',bg='black',command = self.hello)#bg,fg背景前景色,command为按下按钮发生的事件
            self.hi_here.pack()
        def hello(self):
            print("hello")
    
    
    root = tk.Tk()
    App(root)
    
    root.mainloop()

    这次添加一个图片,文字在坐,图片在右边

    from tkinter import *
    import tkinter as tk
    root = tk.Tk()
    
    textlabel = tk.Label(root, text = "非18
    不可观看",#可以转义字符
                         justify = LEFT,#设置左对齐
                         padx = 10)
    textlabel.pack(side = tk.LEFT)
    
    photo = PhotoImage(file = '18.gif')#得到图片,要是gif类型的
    imglabel = tk.Label(root,image = photo)#添加图片
    imglabel.pack(side = tk.RIGHT)
    root.mainloop()

    图片和文字在一起

    from tkinter import *
    import tkinter as tk
    root = tk.Tk()
    photo = PhotoImage(file = '18.gif')#得到图片,要是gif类型的
    textlabel = tk.Label(root, text = "非18
    不可观看",#可以转义字符
                         justify = LEFT,#设置左对齐
                         padx = 10,
                        image = photo,
                        compound = CENTER)#设置为图片和文字混合,图片在中间
    textlabel.pack(side = tk.LEFT)
    root.mainloop()

    图片和文字加按钮,按下按钮文字变换

    from tkinter import *
    
    def change():
        var.set("小孩子不许骗人")
    
    
    root = Tk()
    frame1 = Frame(root)#如果是from tkinter import *,则调用Frame的时候就不用tk.Frame()
    frame2 = Frame(root)
    
    var = StringVar()
    var.set("18才能看,你18了么")
    textlabel = Label(frame1,
                      textvariable = var,#将文字设置成可变的,类型应该为StringVar
                      justify = LEFT)
    textlabel.pack(side = LEFT)
    
    photo = PhotoImage(file = '18.gif')
    imglabel = Label(frame1, image = photo)
    imglabel.pack(side = RIGHT)
    
    button = Button(frame2,text ='我已18',command = change)
    button.pack()
    
    frame1.pack(padx = 10, pady = 10)
    frame2.pack(padx = 10, pady = 10)
    
    mainloop()
  • 相关阅读:
    【UWP】使用Action代替Command
    【UWP】在不同类库使用ResourceDictionaries
    【UWP】不通过异常判断文件是否存在
    【UWP】批量修改图标尺寸
    【UWP】FlipView绑定ItemsSource,Selectedindex的问题
    【UWP】UI适配整理
    【iOS】Object-C注释
    【iOS】desctiption和debugDescription
    【iOS】关联属性存取数据
    【iOS】配置和使用静态库
  • 原文地址:https://www.cnblogs.com/wpbing/p/9817085.html
Copyright © 2011-2022 走看看