按钮的tkinter-子类未从另一个类的内部显示在屏幕上

时间:2020-07-25 22:01:22

标签: python button tkinter subclass

我正在使用tkinter在python中创建Brainf * ck的助手,并且添加了最近的项目部分,但是当我放置按钮时,它们没有出现在屏幕上。

这是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
        print(len(self.buttons))
        for i in range(len(self.buttons)):
            print("placing " + str(self.buttons[i]))
            self.buttons[i].place(x=50,y=100+(20*i))
        self.master.set(title=self.title,width=self.width,height=self.height)
        self.master.master['bg'] = self.color
    def hide(self):
        self.newProject.place_forget()
        self.openProject.place_forget()
    def load(self):
        if not self.loaded:
            self.newProject.place_forget()
            self.openProject.place_forget()
            self.loaded = True
    def unload(self):
        self.newProject.destroy()
        self.openProject.destroy()


def OpenAsk():
    name = File.askopenfilename()

这是main.py的代码:

from tkinter import *
import src.framework.modules.Window.Window as windows
import src.framework.src.Scenes.all as Scenes
import tkinter as tk


root = tk.Tk()
window = windows.window(root, "", 800, 500)
window.place()
projects = open("projects.txt",'r').read().split("\n")

start = Scenes.ProjectPage.ProjectPage(window,projects)
start.show()

window.mainloop()

当我在HoverButton文件的ProjectPage类之外创建ProjectPage时,它按预期出现,但在直接从主文件的类内部初始化时却没有出现。 >

这是一些屏幕截图。

运行main.py的输出:

the output from main

ProjectPage类外部运行并带有左侧代码的输出:

the output and code from outside the class

1 个答案:

答案 0 :(得分:0)

尝试将relx,depend,relwidth,relheight的值作为“地方”的属性插入,或者也可以将height,width作为地方的属性插入。 查阅文档:https://www.tutorialspoint.com/python/tk_place.htm