如何解决全屏显示时如何使标签或框架展开为相对大小
我正在制作啤酒描述的GUI,除了几件事外,其他所有东西都按我的方式工作。当我单击一个按钮时,它会显示一个页面,其中包含啤酒的图片以及以Label形式显示的啤酒说明。当我全屏显示时,文本所在的正方形相对于屏幕会扩大,但是文本保持相同大小。全屏显示时,如何使文本填充标签,而不是其创建的所有多余空间?我尝试了.pack(fill ='both',expand = True)但它似乎没有用。如果有人知道如何对照片做同样的事情,我也将代码简化为只有两个按钮。
import random
import tkinter as tk
from PIL import ImageTk,Image
import sys
American_Pale_Ale = """\nAmerican craft beer movement. American Pale ales are golden to deep amber in color,medium-bodied,and have a moderate-to-high hop flavor. Some favorites include Sierra Nevada or Dale's Pale Ale from Oskar Blues. If there were ever a style most representative of classic American craft beer—this would be it. It's one of the most food-friendly beers,so try it with chicken and fish as well as chili or a cheese plate. """
HEIGHT = 700
WIDTH = 1200
BLUE = '#146F80'
GRAY = '#4A4A4A'
root = tk.Tk()
root.title("Beer App")
root.geometry(f'{WIDTH}x{HEIGHT}+300+150')
# Gets the requested values of the height and width.
windowWidth = root.winfo_reqwidth()
windowHeight = root.winfo_reqheight()
# Gets both half the screen width/height and window width/height
positionRight = int(root.winfo_screenwidth() / 4 - windowWidth / 1)
positionDown = int(root.winfo_screenheight() / 3.5 - windowHeight / 1)
# Positions the window in the center of the page.
root.geometry("+{}+{}".format(positionRight,positionDown))
# make the menu occupy all available width
root.grid_columnconfigure(0,weight=1)
# make the main display container occupy all available height
root.grid_rowconfigure(1,weight=1)
def change_page(name):
global pages,current
if current:
current.place_forget()
current = pages[name][0]
current.place(**pages[name][1])
def menu():
bar = tk.Frame(root,bg=GRAY)
bar.grid(sticky='we')
# bar.grid_columnconfigure(0,weight=1)
opts = dict(bg="#5C665B",fg="black",font=("AR JULIAN",10),activebackground=BLUE,relief='raised',bd=3)
pads = dict(padx=2,pady=16,ipadx=3,ipady=6)
apa_btn = tk.Button(bar,text="American Pale Ale",**opts)
apa_btn.bind("<Button-1>",lambda e: change_page('beer1'))
apa_btn.grid(sticky='w',**pads)
menu_btn = tk.Button(bar,text="Home",**opts)
menu_btn.bind("<Button-1>",lambda e: change_page('main'))
menu_btn.grid(row=0,column=12,**pads)
def main(master):
global images
image = Image.open(r"C:\Users\name\Pictures\PNG\glasses1.png")
image = image.resize((1200,650),Image.ANTIALIAS)
images['mainpic'] = ImageTk.PhotoImage(image,name='mainpic')
tk.Label(display,image='mainpic',bg=BLUE).place(relx=0,rely=0,relheight=1,relwidth=1)
main_frame = tk.Frame(master,bg=GRAY)
welcome_lbl = tk.Label(main_frame,wraplength=680,text='welcome',font=("Gadui",24),bg=GRAY,fg="white",relief='ridge',bd=7)
welcome_lbl.pack(fill='both',expand=True)
return main_frame
def beer1(master):
global images
main_frame = tk.Frame(master,bg=BLUE)
image = Image.open(r"C:\Users\name\Pictures\PNG\american pale ale.png")
images['apa'] = ImageTk.PhotoImage(image,name='apa')
tk.Label(main_frame,image='apa',bg=BLUE).place(relx=-0.08,rely=0.2,relheight=0.7,relwidth=0.5)
beer_desc_lbl = tk.Label(main_frame,text=American_Pale_Ale,justify=tk.LEFT,font=('Gadugi',18),fg='white')
beer_desc_lbl.config(anchor='nw',padx=10)
beer_desc_lbl.place(relx=0.4,rely=0.15,relwidth=0.58)
return main_frame
def close(event):
sys.exit() # if you want to exit the entire thing
# init menu
menu()
# this serves as the master for all content
display = tk.Frame(root,bg=BLUE)
display.grid(sticky='nswe')
# used to keep image references alive
images = dict()
# a reference to every page and it's placement
pages = dict(
main=(main(display),dict(relx=0.2,rely=0.25,relheight=0.5,relwidth=0.6)),beer1=(beer1(display),dict(x=0,y=0,relwidth=1)),)
# a reference to the current page
current = None
# init main page
change_page('main')
# used to exit the program
root.bind('<Escape>',close)
root.mainloop()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。