首页 > 解决方案 > 如何将标签的文本位置移动到标签图像 Tkinter 上方

问题描述

我正在构建一个用于学术用途的 GUI。但是,我有一个带有几个变化框架的主窗口。所有框架都具有相同的背景图像,但只有第一个具有上面的文本 - 起始页。另外两个有他们的文字到图像。我该如何解决它,以便其他两个相同?

import tkinter as tk
# from tkinter import *
from tkinter import ttk, font, filedialog
from PIL import ImageTk, Image
import os


GUI_WIDTH, GUI_HEIGHT = 600, 900
# FONT_STYLE = font.Font(family="Segoe Script", size=10, weight=font.BOLD)
# FONT_STYLE = ("Segoe Script", 10)
BUTTONS_FONT_STYLE = ('Vardine', 11, 'bold')
TITLES_FONT_STYLE = ('Vardine', 21, 'bold')


class App(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.iconbitmap(self, default='afeka logo 3.ico')
        tk.Tk.wm_title(self, 'Detection of colon tumors through ultrasound images')

        container = tk.Frame(self)
        container.pack(side='top', fill='both', expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (StartPage, OverviewPage, LaunchPage):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky='nsew')

        self.show_frame(StartPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()


def openDocxFile():
    os.startfile("Final_Project.docx")


def uploadImage(event=None):
    filename = filedialog.askopenfilename()
    if filename:
        print('Selected:', filename)


def adjustImage(event):
    label = event.widget
    width = event.width
    height = event.height
    image = Image.open('background1.png')
    resizedImage = image.resize((width, height))
    newImage = ImageTk.PhotoImage(resizedImage)
    label.config(image=newImage)
    label.image = newImage


class StartPage(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

        self.image = tk.PhotoImage(file='background1.png')
        label = ttk.Label(self, text='Start Page', image=self.image, font=TITLES_FONT_STYLE,  relief='raised', compound='bottom')
        label.bind('<Configure>', adjustImage)
        label.pack(expand=True)

        button_overview_page = tk.Button(self, text='overview', font=BUTTONS_FONT_STYLE,
                                         command=lambda: controller.show_frame(OverviewPage))
        button_overview_page.place(relx=0.35, rely=0.2, relwidth=0.3, relheight=0.1)
        button_doc_file = tk.Button(self, text='docx file', font=BUTTONS_FONT_STYLE, command=openDocxFile)
        button_doc_file.place(relx=0.35, rely=0.4, relwidth=0.3, relheight=0.1)

        button_launch_page = tk.Button(self, text='launch page', font=BUTTONS_FONT_STYLE,
                                       command=lambda: controller.show_frame(LaunchPage))
        button_launch_page.place(relx=0.35, rely=0.6, relwidth=0.3, relheight=0.1)
        button_exit = tk.Button(self, text='exit', font=BUTTONS_FONT_STYLE, command=quit)
        button_exit.place(relx=0.35, rely=0.8, relwidth=0.3, relheight=0.1)


class OverviewPage(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

        self.image = tk.PhotoImage(file='background1.png')

        label = tk.Label(self, text="Overview Page", image=self.image, font=TITLES_FONT_STYLE,  relief='raised',
                         compound='bottom')
        label.bind('<Configure>', adjustImage)
        label.pack(expand=True)

        button_start_page = tk.Button(self, text='start page', font=BUTTONS_FONT_STYLE,
                                      command=lambda: controller.show_frame(StartPage))
        button_start_page.place(relx=0.35, rely=0.6, relwidth=0.3, relheight=0.1)
        # button_launch_page = tk.Button(self, text='launch page', font=BUTTONS_FONT_STYLE,
        #                                command=lambda: controller.show_frame(LaunchPage))
        # button_launch_page.place(relx=0.35, rely=0.4, relwidth=0.3, relheight=0.1)
        button_exit = tk.Button(self, text='exit', font=BUTTONS_FONT_STYLE, command=quit)
        button_exit.place(relx=0.35, rely=0.8, relwidth=0.3, relheight=0.1)


class LaunchPage(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

        self.image = tk.PhotoImage(file='background1.png')

        label = tk.Label(self, text="Launch Page", image=self.image, font=TITLES_FONT_STYLE,  relief='raised', compound='bottom')
        label.bind('<Configure>', adjustImage)
        label.pack(expand=True)

        button_start_page = tk.Button(self, text='start page', font=BUTTONS_FONT_STYLE,
                                      command=lambda: controller.show_frame(StartPage))
        button_start_page.place(relx=0.35, rely=0.6, relwidth=0.3, relheight=0.1)
        # button_overview_page = tk.Button(self, text='overview', font=BUTTONS_FONT_STYLE,
        #                                command=lambda: controller.show_frame(OverviewPage))
        # button_overview_page.place(relx=0.35, rely=0.3, relwidth=0.3, relheight=0.1)
        button_upload_image = tk.Button(self, text='upload image', font=BUTTONS_FONT_STYLE,
                                        command=lambda: [uploadImage(), button_launch_detection.config(text='launch detection', state='normal')])
        button_upload_image.place(relx=0.35, rely=0.2, relwidth=0.3, relheight=0.1)
        button_launch_detection = tk.Button(self, text='upload an image\nto enable launching', font=BUTTONS_FONT_STYLE)
        button_launch_detection.place(relx=0.35, rely=0.4, relwidth=0.3, relheight=0.1)
        button_launch_detection.config(state='disabled')
        button_exit = tk.Button(self, text='exit', font=BUTTONS_FONT_STYLE, command=quit)
        button_exit.place(relx=0.35, rely=0.8, relwidth=0.3, relheight=0.1)


app = App()
# Define geometry - size and position
# Center app window on the screen
screen_width = app.winfo_screenwidth()
screen_height = app.winfo_screenheight()
center_x = int(screen_width / 2 - GUI_WIDTH / 2)
center_y = int(screen_height / 2 - GUI_HEIGHT / 2)
app.geometry(f'{GUI_WIDTH}x{GUI_HEIGHT}+{center_x}+{center_y}')
# # Decrease factor to change window's transparency
# app.attributes('-alpha', 1)
# Create sizegrip

app.mainloop()

在此处输入图像描述2在此处 输入图像描述

标签: pythonuser-interfacetkinter

解决方案


推荐阅读