tkinter-按钮的子类不从另一个类内部放置在屏幕上

我正在使用 tkinter 在 python 中为 brainf*ck 制作一个 ide,我正在添加一个最近的项目部分,但是当我放置按钮时,它们不会出现在屏幕上。


这是代码Scene:


from tkinter import *

from tkinter import filedialog as File

import tkinter as tk



class HoverButton(tk.Button):

    def __init__(self, master, **kw):

        tk.Button.__init__(self, master=master, **kw)

        self.defaultBackground = "#5d5d5d"

        self['background'] = self.defaultBackground

        self['activebackground'] = "#6d6d6d"

        self.bind("<Enter>", self.on_enter)

        self.bind("<Leave>", self.on_leave)

    def on_enter(self, e):

        self['background'] = "#6d6d6d"


    def on_leave(self, e):

        self['background'] = self.defaultBackground



class ProjectPage(Frame):

    def __init__(self, master, projects=[]):

        super().__init__(master)

        self.projects = projects

        self.buttons = []

        self.mstr = self.master.master

        self.title = "PesolIde: Projets"

        self.width = 800

        self.height = 500

        self.color = "#4d4d4d"

        self.projectFrame = tk.Frame(self.mstr,width=800,height=50,bg="#5d5d5d")

        self.newProject = HoverButton(self.mstr,text="New Project", height=1, bg="#6d6d6d")

        self.openProject = HoverButton(self.mstr,text="Open Project", height=1,bg="#6d6d6d", command=OpenAsk)

        self.projectdisplay = tk.Frame(self.mstr, width=700, height=300, bg="#5d5d5d", highlightbackground="black", highlightthickness=1)

        for i in range(len(self.projects)):

            self.buttons.append(HoverButton(master, text=self.projects[i].split(':')[0], width=50, height=1))

            if len(self.buttons)>=40:

                break

        self.loaded = False

    def show(self):

        self.projectFrame.place(x=0, y=0)

        self.newProject.place(x=20, y=10)

        self.openProject.place(x=120, y=10)

        self.projectdisplay.place(x=50,y=100)

        self.y = 100



当我在文件中创建类HoverButton外部时,它会按预期显示,但在直接从主文件的类中初始化时不会出现。ProjectPageProjectPage


http://img3.mukewang.com/642551f90001015706540435.jpg

ProjectPage使用左侧代码从类外运行的输出:

http://img4.mukewang.com/642552090001d61a06470139.jpg

回首忆惘然
浏览 82回答 1
1回答

MYYA

尝试插入 relx、rely、relwidth、relheight 的值作为“place”中的属性,或者您也可以插入 height、width 作为 place 的属性。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python