当前位置:   article > 正文

Python图形界面设计

python图形界面

1、按钮对话框,并显示输入对应的内容

from tkinter import *
import tkinter.messagebox as messagebox

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

    def createWidgets(self):
        self.nameInput = Entry(self)
        self.nameInput.pack()
        self.alertButton = Button(self, text='Hello', command=self.hello)
        self.alertButton.pack()

    def hello(self):
        name = self.nameInput.get() or 'world'
        messagebox.showinfo('Message', 'CT/TE, %s' % name)

app = Application()
# 设置窗口标题:
app.master.title('独爱空城梦')
# 主消息循环:
app.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

在这里插入图片描述
2、文本框,显示相对应的文字

import tkinter as tk

app = tk.Tk()
app.title("FishC Demo")
#标签控件;可以显示文本和位图
theLable = tk.Label(app, text="你若安好,便是晴天",width=20,height=10)
theLable.pack()

app.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在这里插入图片描述
3、按钮与文本框,运行该代码时,点击按钮,在控制台会输出相对应的文字

import tkinter as tk
class App: #定义类
    
    def __init__(self, master):
        frame = tk.Frame(master)#定义窗体框架
        frame.pack(side=tk.RIGHT, padx=100,pady=100)
        
        #定义按钮
        self.hi_there = tk.Button(frame, text="打招呼",bg="red" ,fg='green',command=self.say_hi)
        self.hi_there.pack(padx=10, pady=10)#位置
        
    def say_hi(self):
        print("嗨,好久不见,你可安好!")
             
root = tk.Tk()
app = App(root)
root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

在这里插入图片描述
在这里插入图片描述
4、制作一个动态的五颜六色的树

from turtle import *
# 设置色彩模式是RGB:
colormode(255)
lt(90)
lv = 14
l = 120
s = 45
width(lv)
# 初始化RGB颜色:
r = 0
g = 0
b = 0
pencolor(r, g, b)

penup()
bk(l)
pendown()
fd(l)

def draw_tree(l, level):
    global r, g, b
    # save the current pen width
    w = width()

    # narrow the pen width
    width(w * 3.0 / 4.0)
    # set color:
    r = r + 1
    g = g + 2
    b = b + 3
    pencolor(r % 200, g % 200, b % 200)

    l = 3.0 / 4.0 * l

    lt(s)
    fd(l)

    if level < lv:
        draw_tree(l, level + 1)
    bk(l)
    rt(2 * s)
    fd(l)

    if level < lv:
        draw_tree(l, level + 1)
    bk(l)
    lt(s)
    width(w)
    
speed("fastest")
draw_tree(l, 4)
done()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

在这里插入图片描述

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/874748
推荐阅读
相关标签
  

闽ICP备14008679号