我用的是python的tkinter包    ——在python3.*版本以上,tkinter包T是小写

1、首先了解一下tkinter的最基本的绘制:

from tkinter import *
#导入包名  这样的方式方便不用tkinter.出来他的功能

root = Tk()
#Tk()是根窗口,所有的子窗口都是来源于它

root.title("根窗口的标题")
#title标题

root.geometry('600x300')
#根窗口的geometry几何结构|窗口大小

label_a = Label(root,text = '文字')
#Label标签控件:显示文本和位图  root:Label继承自root控件|Label是root的子属关系 text是Label里面的一些属性,当然label的属性还有width……很多

#要想把label_a实例出来,要经过以下三种的其中一种
#label_a.pack() #pack包装 pack(还可以加包装的属性|怎么包装)
#label_a.grid() #grid格子
#label_a.place() #place放置 place(x=10,y=10,width=80 ,height=20)放置在什么位置,放置的大小

label_a.place(x=10,y=10,width=80 ,height=20)

root.mainloop()
#因为窗口是一直存在的所以要加一个mainloop循环

绘制结果:

python的GUI基础绘制

2、给窗体添加菜单栏:

#添加菜单
from tkinter import *
root = Tk()
root.title('这就是标题')
root.geometry('600x300')

mater = Menu(root)
#Menu菜单控件|主菜单
root.config(menu=mater)
#config配置 


##command命令++具体功能+++++
def fonution():
    print("file")
def fonution_a():
    print("help")
##++++++++++++具体功能++++++


file_menu = Menu(mater)
#file_menu菜单基于mater主菜单
mater.add_cascade(label = '|file|',menu = file_menu)
#菜单. add_cascade添加层级(标识名 ,菜单)
file_menu.add_command(label = 'new',command = fonution)
#file_menu菜单add_command添加命令(标识,命令=函数(功能))
file_menu.add_separator()
#添加一个分隔符
file_menu.add_command(labe = 'open',command = fonution)
#file_menu添加一个新的命令


##添加一个新的菜单栏++++++++++
help_menu = Menu(mater)
mater.add_cascade(label = '|help|',menu = help_menu)
help_menu.add_command(label = '相关信息',command = fonution_a)
help_menu.add_separator()
help_menu.add_command(label = '联系方式',command = fonution_a)
##+++++++++新菜单栏++++++++++

root.mainloop()

输出结果:

python的GUI基础绘制

 

3、按钮|登入界面制作:

#登录界面演示
from tkinter import *
from tkinter import messagebox

class app(object):
    def __init__(self, master):
        self.master = master
        self.Master()
    def Master(self):
        ##登录ID++++++++++++++++++++++
        l1 = Label(self.master,text = 'User ID:',font = ('bold'))
        l1.place(x = 10,y = 10,width = 80,height = 20)
        
        e1_text = StringVar(self.master,value = '')
        #定义一个保存Entry登记textvariable信息的变量 要得到它的值要get一下
        e1 = Entry(self.master,textvariable = e1_text)
        e1.place(x = 100,y = 10,width = 80,height = 20)
        #++++++++++++++++++++++++++++

        ##登录pwd++++++++++++++++++
        l2 = Label(self.master,text = 'UserPWD:',font = ('bold'))
        l2.place(x = 10,y = 30,width = 80,height = 20)
        
        e2_text = StringVar(self.master,value = '')
        e2 = Entry(self.master,textvariable = e2_text)
        e2.place(x = 100,y = 30,width = 80,height = 20)
        #++++++++++++++++++++++++++++

        def command_b1():
            eid = e1_text.get()
            #get得到textvariable信息的回馈
            epwd = e2_text.get()
            if eid == epwd:
                messagebox.showinfo(title = 'ok',message = 'OK')
                #弹出消息框
            else:
                messagebox.showinfo(title = 'no',message = 'NO')
        b1 = Button(self.master,text = '按钮',command = command_b1)
        #Button按钮控件  点击按钮后的command命令
        b1.place(x = 50,y = 50,width = 80,height = 20)
 

root = Tk()
root.title('这就是标题')
root.geometry('600x300')
a = app(root)

root.mainloop()

输出结果:

python的GUI基础绘制

4、Label添加图片

from tkinter import *
from PIL import Image,ImageTk

root = Tk()
root.geometry('600x430')

img_address = r'..\图片\幽灵姬.jpg'
#图片地址
img = Image.open(img_address)
#打开图片地址
img_tk=ImageTk.PhotoImage(img)
#实例图片内容

l1=Label(root,image = img_tk)
l1.place(x=5,y=10 ,width=300 ,height=400)

root.mainloop()

输出结果:

python的GUI基础绘制

相关文章:

  • 2022-01-10
  • 2021-09-19
  • 2021-04-23
  • 2021-05-24
  • 2021-04-19
  • 2022-12-23
  • 2022-12-23
  • 2021-04-27
猜你喜欢
  • 2021-08-07
  • 2022-02-25
  • 2021-05-07
  • 2022-12-23
  • 2021-06-30
  • 2022-02-18
  • 2021-12-03
相关资源
相似解决方案