tkinter类和方法中的标签布局

说明:
此处主要解决在类中各个方法不同标签之间的统一布局问题
在代码中有备注的步骤为重点和关键步骤
代码如下:

import tkinter as tk
from PIL import Image,ImageTk

class buju():
    def __init__(self):
        d=0

    def la(self):
        #frm=tk.Frame(root)
        frm.pack(side=tk.BOTTOM)
        tk.Label(root,text='厚德'.encode('utf-8').decode('utf-8'), font=('Arial', 15)).pack()
        a=tk.Label(frm_Left,text='载物'.encode('utf-8').decode('utf-8'), font=('Arial', 15)).pack()  #放在相应的frm_Left布局内
        b=tk.Label(frm_Left, text='博爱'.encode('utf-8').decode('utf-8'), font=('Arial', 15)).pack()
        frm_Left.pack(side=tk.LEFT)  #并且结束后一定要将这个布局模块pack下

        tk.Label(frm_R, text='敬业'.encode('utf-8').decode('utf-8'), font=('Arial', 15)).pack()
        tk.Label(frm_R, text='乐群'.encode('utf-8').decode('utf-8'), font=('Arial', 15)).pack()

        img = r"F:\PythonFiles\PycharmFile\pictures\ex13RandomNameWithPic\01.png"  
        photo = Image.open(img)  
        w, h = photo.size 
        resizePhoto = self.pic_resize(w, h, 153, 153, photo) 
        tkPhoto = ImageTk.PhotoImage(resizePhoto) 
        labelImg = tk.Label(frm_R, image=tkPhoto)
        labelImg.pack()
        labelImg.config(image=tkPhoto)  # 以下两句代码非常重要,是保证图在函数中可以被加载的途径
        labelImg.image = tkPhoto
        labelImg.pack()

        frm_R.pack(side=tk.RIGHT)

    def pic_resize(self, w, h, w_box, h_box, pil_image): #图形缩放
        f1 = 1.0 * w_box / w  # 1.0 forces float division in Python2
        f2 = 1.0 * h_box / h
        factor = min([f1, f2])
        width = int(w * factor)
        height = int(h * factor)
        return pil_image.resize((width, height), Image.ANTIALIAS)

if __name__ == '__main__':
    root=tk.Tk()
    root.title('随机')
    root.geometry('300x200')
    frm=tk.Frame(root)  #窗体布局
    frm_Left=tk.Frame(frm)  #窗体布局的各个小模块均可以在主函数里写,然后再类里直接调用就行
    frm_R=tk.Frame(frm)
    t=buju()
    t.la()

    root.mainloop()


你可能感兴趣的:(软件类)