个人使用Python Tk GUI认为较有价值的一些笔记

发布于:2023-01-11 ⋅ 阅读:(464) ⋅ 点赞:(0)

import tkinter (定义Tk窗口)

-----------------------------------------------------------------------------------------------------------------

from tkinter.ttk import Label (定义Tk窗口的文字)

-----------------------------------------------------------------------------------------------------------------

top = tkinter.Tk() (定义窗口并赋值到top上)

-----------------------------------------------------------------------------------------------------------------

top.geometry(“    x    ”) (定义窗口大小)

-----------------------------------------------------------------------------------------------------------------

top.title(“             ” ) (定义窗口文字)

-----------------------------------------------------------------------------------------------------------------

tkinter.Label(top, text="      ").pack() (文字显示定义)

-----------------------------------------------------------------------------------------------------------------

top.iconbitmap('     .ico')  (定义窗口图标(ico64*64)))

-----------------------------------------------------------------------------------------------------------------

window.tk.call('wm', 'iconphoto', window._w, tk.PhotoImage(file='   .png'))  更改窗口图标(jpg))

-----------------------------------------------------------------------------------------------------------------

index.maxsize(500,300) (固定最大大小)

index.minsize(500,300)  (固定最小大小)

-----------------------------------------------------------------------------------------------------------------

image_file = tkinter.PhotoImage(file="114514.gif")

image = canvas.create_image(250, 0, anchor='n', image=image_file)

canvas.pack()   (加载图片)

-----------------------------------------------------------------------------------------------------------------

account_lable = tkinter.Label(index, text = '账号', bg='lightskyblue', fg='white', font=('Arial', 12), width=5, height=1)

account_lable.place(relx=0.29,rely=0.4)

pasw_lable = tkinter.Label(index, text = '密码', bg='lightskyblue', fg='white', font=('Arial', 12), width=5, height=1)

pasw_lable.place(relx=0.29,rely=0.5)   (账号与密码文字标签)

-----------------------------------------------------------------------------------------------------------------

account = tkinter.Entry(index,width=20,highlightthickness = 1,highlightcolor = 'lightskyblue',relief='groove')  #账号输入框

account.place(relx=0.4,rely=0.4 )  #添加进主页面,relxrely意思是与父元件的相对位置

password = tkinter.Entry(index,show='*',highlightthickness = 1,highlightcolor = 'lightskyblue',relief='groove')  #密码输入框

password.place(relx=0.4,rely=0.5) #添加进主页面   (账号与密码输入框)

-----------------------------------------------------------------------------------------------------------------

def login():

    ac = account.get()

    ps = password.get()

    if (ac == "" or ps == ""):

        showinfo("用户登录", "请完整填写信息!")

    elif user.get(ac) != ps:

        account.delete(0,'end')  #清空文本框的内容

        password.delete(0,'end')  #清空文本框的内容

        showinfo("用户登录", "账号或者密码有误!")

        account.delete(0, 'end')  # 清空文本框的内容

        password.delete(0, 'end')  # 清空文本框的内容

    else:

        showinfo("用户登录", "登录成功!即将进入菜单界面....")

        打开超链接('http://crepika.freeee.ml/')

def reguest():

    ac = account.get()

    ps = password.get()

    if (ac == "" or ps == ""):

        showinfo("用户登录", "请完整填写信息!别闹!")

    elif ac in user:

        account.delete(0,'end')  #清空文本框的内容

        password.delete(0,'end')  #清空文本框的内容

        showinfo("用户注册", "账号已存在!你隔这玩呢?") 

    else:

        user[ac] = ps

        account.delete(0, 'end')  # 清空文本框的内容

        password.delete(0, 'end')  # 清空文本框的内容

        showinfo("用户注册", "注册成功!")

(登录按钮处理函数)

-----------------------------------------------------------------------------------------------------------------

loginBtn = tkinter.Button(index,text='登录',font = ('宋体',12),width=4,height=1,command=login,relief='solid',bd = 0.5,bg='lightcyan')

loginBtn.place(relx=0.41,rely=0.63)

loginBtn = tkinter.Button(index,text='注册',font = ('宋体',12),width=4,height=1,bd=0.5,command=reguest,relief='solid',bg='lightcyan')

loginBtn.place(relx=0.56,rely=0.63)

(登录与注册按钮)

-----------------------------------------------------------------------------------------------------------------

top.mainloop() (执行窗口)

-----------------------------------------------------------------------------------------------------------------

from os import system

def 打开超链接(url):

system('start {}'.format(url))

打开超链接('http://crepika.freeee.ml/')

桀皮

(打开超链接命令(cmd))

-----------------------------------------------------------------------------------------------------------------

import turtle

turtle.setup(width=500,height=200)

turtle.speed(1)

turtle.hideturtle()

turtle.penup()

turtle.back(100)

turtle.write("桔子code", align="right", font=("宋体", 22, "bold"))

turtle.mainloop()

(用Pythonturtle库在画布上写字)

-----------------------------------------------------------------------------------------------------------------

name_label.grid(row=0,column=0) # grid  #方式布局,通过设置行列的方式布局,从0开始

name_entry.grid(row=0,column=1)

pwd_label.grid(row=1,column=0)

pwd_entry.grid(row=1,column=1)

login_btn.grid(row=2,column=1)

(采用grid方式进行布局)

-----------------------------------------------------------------------------------------------------------------

name_label.grid(row=0, column=0, padx=20, pady=20)   # grid 方式布局,通过设置行列的方式布局,从0开始

name_entry.grid(row=0, column=1)

pwd_label.grid(row=1, column=0)

pwd_entry.grid(row=1, column=1)

login_btn.grid(row=2, column=1, pady=20, stick=E)  # stick表示靠哪边对齐,通过N(北)S(南)W(西)E(东)来表示

(优化grid布局)

-----------------------------------------------------------------------------------------------------------------

编写:桀皮

本文含有隐藏内容,请 开通VIP 后查看