Python 3.x 使用Tkinter在顶层窗口上调整背景图像
我有一个基本的GUI,它以各种主菜单开始。我已经成功地为该菜单设置了一个背景图像,当我更改GUI窗口的尺寸时,它也会缩放。 但是,当我试图定义一些由子菜单项打开的顶级窗口时,背景图像不会出现(更不用说比例)。 不确定我做错了什么,但我附加了我编写的代码以及基本GUI的图像Python 3.x 使用Tkinter在顶层窗口上调整背景图像,python-3.x,user-interface,tkinter,Python 3.x,User Interface,Tkinter,我有一个基本的GUI,它以各种主菜单开始。我已经成功地为该菜单设置了一个背景图像,当我更改GUI窗口的尺寸时,它也会缩放。 但是,当我试图定义一些由子菜单项打开的顶级窗口时,背景图像不会出现(更不用说比例)。 不确定我做错了什么,但我附加了我编写的代码以及基本GUI的图像 from tkinter import * from tkinter import ttk, font, messagebox from PIL import ImageTk, Image import os root =
from tkinter import *
from tkinter import ttk, font, messagebox
from PIL import ImageTk, Image
import os
root = Tk()
root.title("Decoder of ultrasound images to detect colon tumors")
# Adding window icon
root.iconbitmap('afekaImage.ico')
rootWidth, rootHeight = 600, 600
screenWidth = root.winfo_screenwidth()
screenHeight = root.winfo_screenheight()
topLeftPosition = (screenWidth / 2 - rootWidth / 2, screenHeight / 2 - rootHeight / 2)
# Configure window size
root.geometry(f'{rootWidth}x{rootHeight}+{int(topLeftPosition[0])}+{int(topLeftPosition[1])}')
'''
# Create username & password entry
def entryDialog():
userName = entry1.get()
password = entry2.get()
if ((userName == 'Itzhak.Mamistvalov' and password == '311396832') or
(userName == 'AssafHasky' and password == '308333533')):
messagebox.showinfo('info', 'Correct Login')
else:
messagebox.showinfo('info', 'Invalid Login') '''
# open doc file
def openDocFile():
os.startfile("mid_sub.docx")
# adjusting background image to fit window
def adjustBackgroundImage(event):
# avoid garbage collection option 1
# global resizedBackgroundImage, newBackgroundImage
# ----------
width = event.width
height = event.height
resizedBackgroundImage = copyImage.resize((width, height))
newBackgroundImage = ImageTk.PhotoImage(resizedBackgroundImage)
label.config(image=newBackgroundImage)
# avoid garbage collection option 2
label.image = newBackgroundImage
# ----------
def createUserManualWindow(button_userManual):
global image1
userManualWindow = Toplevel(root)
def activateButtonUserManual():
button_userManual.configure(state="normal")
userManualWindow.destroy()
button_userManual.configure(state="disabled")
button_exit_userManualWindow = Button(userManualWindow, text="Exit", font=fontStyle,
command=lambda: [userManualWindow.destroy(), activateButtonUserManual()])
button_exit_userManualWindow.place(relx=0.4, rely=0.8, relwidth=0.2, relheight=0.1)
# will occurs only when esc pressed
userManualWindow.protocol("WM_DELETE_WINDOW", activateButtonUserManual)
# ----------
userManualWindow.geometry(f'{rootWidth}x{rootHeight}+{int(topLeftPosition[0])}+{int(topLeftPosition[1])}')
userManualWindow.iconbitmap('afekaImage.ico')
image1 = ImageTk.PhotoImage(Image.open('background.jpg'))
label1 = ttk.Label(userManualWindow, image=image1).pack()
label1.bind('<Configure>', adjustBackgroundImage)
label1.pack(fill=BOTH, expand=YES)
def createOverviewWindow(button_userManual):
overviewWindow = Toplevel(root)
def activateButtonOverview():
button_userManual.configure(state="normal")
overviewWindow.destroy()
button_userManual.configure(state="disabled")
button_exit_OverviewWindow = Button(overviewWindow, text="Exit", font=fontStyle,
command=lambda: [overviewWindow.destroy(), activateButtonOverview()])
button_exit_OverviewWindow.place(relx=0.4, rely=0.8, relwidth=0.2, relheight=0.1)
# will occurs only when esc pressed
overviewWindow.protocol("WM_DELETE_WINDOW", activateButtonOverview)
# ----------
overviewWindow.geometry(f'{rootWidth}x{rootHeight}+{int(topLeftPosition[0])}+{int(topLeftPosition[1])}')
overviewWindow.iconbitmap('afekaImage.ico')
# Define background image
image = Image.open('background.jpg')
copyImage = image.copy()
backgroundImage = ImageTk.PhotoImage(image)
label = ttk.Label(root, image=backgroundImage)
label.bind('<Configure>', adjustBackgroundImage)
label.pack(fill=BOTH, expand=YES)
# Configure font
fontStyle = font.Font(family="Segoe Script", size=10, weight=font.BOLD)
# Create buttons
button_userManual = Button(root, text="USER MANUAL", command=lambda: createUserManualWindow(button_userManual), font=fontStyle)
button_userManual.place(relx=0.4, rely=0.2, relwidth=0.2, relheight=0.1)
button_overview = Button(root, text="OVERVIEW", command=lambda: createOverviewWindow(button_overview), font=fontStyle)
button_overview.place(relx=0.4, rely=0.4, relwidth=0.2, relheight=0.1)
button_openDocFile = Button(root, text="DOC FILE", font=fontStyle, command=openDocFile)
button_openDocFile.place(relx=0.4, rely=0.6, relwidth=0.2, relheight=0.1)
button_quit = Button(root, text="Exit", font=fontStyle, command=root.destroy)
button_quit.place(relx=0.4, rely=0.8, relwidth=0.2, relheight=0.1)
root.mainloop()
从tkinter导入*
从tkinter导入ttk、字体、消息框
从PIL导入ImageTk,图像
导入操作系统
root=Tk()
root.title(“检测结肠肿瘤的超声图像解码器”)
#添加窗口图标
root.iconbitmap('afekaImage.ico'))
rootWidth,rootHeight=600600
screenWidth=root.winfo_screenWidth()
screenHeight=root.winfo_screenHeight()
topLeftPosition=(屏幕宽度/2-rootWidth/2,屏幕高度/2-rootHeight/2)
#配置窗口大小
几何体(f'{rootWidth}x{rootHeight}+{int(topLeftPosition[0])}+{int(topLeftPosition[1])})
'''
#创建用户名和密码条目
def entryDialog():
userName=entry1.get()
password=entry2.get()
如果((用户名='Itzhak.Mamistvalov'和密码='311396832')或
(用户名==“AssafHasky”和密码==“308333533”):
messagebox.showinfo('info','Correct Login')
其他:
messagebox.showinfo('info','Invalid Login')“”
#打开文档文件
def openDocFile():
os.startfile(“mid_sub.docx”)
#调整背景图像以适应窗口
def调整背景图像(事件):
#避免垃圾收集选项1
#全局调整BackgroundImage,newBackgroundImage
# ----------
宽度=事件宽度
高度=事件高度
resizedBackgroundImage=copyImage.resize((宽度、高度))
newBackgroundImage=ImageTk.PhotoImage(大小为BackgroundImage)
label.config(image=newBackgroundImage)
#避免垃圾收集选项2
label.image=newBackgroundImage
# ----------
def createUserManualWindow(按钮用户手册):
全球图像1
userManualWindow=Toplevel(根目录)
def ActivateButtonServerManual():
按钮\u userManual.configure(state=“normal”)
userManualWindow.destroy()
按钮\u userManual.configure(state=“disabled”)
按钮\退出\用户手册窗口=按钮(用户手册窗口,text=“退出”,font=fontStyle,
command=lambda:[userManualWindow.destroy(),activateButtonUserManual()]
按钮\退出\用户手册窗口位置(relx=0.4,rely=0.8,relwidth=0.2,relheight=0.1)
#将仅在按下esc键时发生
协议(“WM_删除_窗口”,激活按钮用户手册)
# ----------
几何体(f'{rootWidth}x{rootHeight}+{int(topLeftPosition[0])}+{int(topLeftPosition[1])})
userManualWindow.iconbitmap('afekaImage.ico')
image1=ImageTk.PhotoImage(Image.open('background.jpg'))
label1=ttk.Label(userManualWindow,image=image1.pack())
标签1.绑定(“”,调整背景图像)
标签1.pack(填充=两者,扩展=是)
def CreateOverview窗口(按钮\用户手册):
概览窗口=顶层(根)
def activateButtonOverview():
按钮\u userManual.configure(state=“normal”)
overviewWindow.destroy()
按钮\u userManual.configure(state=“disabled”)
按钮\退出\概览窗口=按钮(概览窗口,text=“exit”,font=fontStyle,
command=lambda:[overviewWindow.destroy(),activateButtonOverview()]
按钮\退出\概览窗口。放置(relx=0.4,rely=0.8,relwidth=0.2,relheight=0.1)
#将仅在按下esc键时发生
概述窗口。协议(“WM_删除_窗口”,激活按钮概述)
# ----------
概述窗口.geometry(f'{rootWidth}x{rootHeight}+{int(topLeftPosition[0])}+{int(topLeftPosition[1])})
概览窗口.iconbitmap('afekaImage.ico'))
#定义背景图像
image=image.open('background.jpg')
copyImage=image.copy()
backgroundImage=ImageTk.PhotoImage(图像)
label=ttk.label(root,image=backgroundImage)
标签绑定(“”,调整背景图像)
label.pack(填充=两者,展开=是)
#配置字体
fontStyle=font.font(family=“Segoe Script”,size=10,weight=font.BOLD)
#创建按钮
按钮\用户手册=按钮(root,text=“USER MANUAL”,command=lambda:createUserManualWindow(按钮\用户手册),font=fontStyle)
按钮_userManual.place(relx=0.4,rely=0.2,relwidth=0.2,relheight=0.1)
按钮概述=按钮(root,text=“overview”,command=lambda:createOverview窗口(按钮概述),font=fontStyle)
按钮概述。放置(relx=0.4,rely=0.4,relwidth=0.2,relheight=0.1)
按钮\u openDocFile=button(root,text=“DOC FILE”,font=fontStyle,command=openDocFile)
按钮_openDocFile.place(relx=0.4,rely=0.6,relwidth=0.2,relheight=0.1)
按钮退出=按钮(root,text=“Exit”,font=fontStyle,command=root.destroy)
按钮退出位置(relx=0.4,rely=0.8,relwidth=0.2,relheight=0.1)
root.mainloop()
label1=ttk.Label(userManualWindow,image=image1).pack()
应更改为:label1=ttk.Label(用户手册窗口,图像=image1)
标签1.pack()
label1.pack()
,否则它将重叠/隐藏“退出”按钮。或者在label1.pack()之后调用label1.lower()
label
用于adjustBackgroundImage()
,因此即使将label1
上的
绑定到adjustBackgroundImage()
,它也不会调整label1
显示的图像的大小。使用event.widget
而不是label
内部adjustBackgroundImage()
:
def adjustBackgroundImage(事件):
label=event.widget
#避免垃圾收集选项1
#全局调整BackgroundImage,newBackgroundImage
# ----------
宽度=事件宽度
高度=事件高度
调整BackgroundImage的大小