展开页面顶部的 canvas

Expand canvas at the top of page

我有这段代码,在起始页的顶部有一个菜单,其中提供了一系列按钮 'Home Task YourAdressess Payment',我将它们全部放在红色 canvas ,它的形状像横幅,因此可以突出显示。我希望 canvas 在用户加宽或缩小 window 时伸展,我做到了,但由于某种原因 canvas 只是停留在页面的中间而不是顶部,拜托帮助。这部分代码在'Class StartPage(tk.Frame)'

from tkinter import *
import time
import tkinter as tk
LARGE_FONT= ("Verdana", 12)

class SeaofBTCapp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        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, HypeExtractor, Task, Payment, YourAdressess):

            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):
        #for frame in self.frames.values():
            #frame.grid_remove()

        frame = self.frames[cont]
        frame.tkraise()
        frame.winfo_toplevel().geometry("1024x720")

#The Bit 
class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        #The Bit I Need Help On 
        c = Canvas(self, height=50, width=1024, bg="red")
        c.pack(fill = 'x',expand = True)
        homebutton = tk.Button(self, text='Home', command=lambda: controller.show_frame(HypeExtractor))
        homebutton_window = c.create_window(10, 12.5, anchor=tk.NW, window=homebutton)
        taskbutton = tk.Button(self, text='Task', command=lambda: controller.show_frame(Task))
        taskbutton_window = c.create_window(104, 12.5, anchor=tk.NW, window=taskbutton)
        adressbutton = tk.Button(self, text='Your Adressess', command=lambda: controller.show_frame(YourAdressess))
        adressbutton_window = c.create_window(196, 12.5, anchor=tk.NW, window=adressbutton)
        paymentbutton = tk.Button(self, text='Payment', command=lambda: controller.show_frame(Payment))
        paymentbutton_window = c.create_window(323, 12.5, anchor=tk.NW, window=paymentbutton)

        # usernameentry.delete(0, END)
        # passwordnameentry.delete(0, END)

class HypeExtractor(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

class Task(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

class YourAdressess(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
class Payment(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

app = SeaofBTCapp()
app.mainloop()

您需要将 expand 设置为 False,因为您不希望它扩展以填充所有额外的 space。最好包括 side="top",即使这是默认设置,因为它使您的意图更加明确。