全屏显示时如何使标签或框架展开为相对大小

如何解决全屏显示时如何使标签或框架展开为相对大小

我正在制作啤酒描述的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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-