У меня есть ошибка на моей странице, которая не позволяет отображать ее в правильном положении - PullRequest
0 голосов
/ 01 апреля 2019

Итак, я построил программу, которая представляет собой простую систему страниц, несколько функций, таких как динамическое добавление кнопок для новых страниц, полоса прокрутки для содержимого и т. Д. Но у меня возникает странная проблема с моей домашней страницей. Вот гифка с проблемой:

https://gyazo.com/cb42cbab30fa7754127854589e7a288f

Как только я упаковываю домашнюю страницу pack_forget (), а затем снова упаковываю ее (), она появляется на экране ниже. Я подумал, что это мог быть элемент на главной странице, что привело к его снижению, но это не так, я удалил весь контент со страницы, и проблема не устранена, и другие страницы, похоже, не затрагиваются. Проблема сохраняется, если другие страницы полностью удаляются из программы, поэтому они никоим образом не отталкивают ее.

У меня нет никаких других фреймов, кроме страниц, которые наследуются классом Page, и если я удаляю все из них, а также содержимое для домашней страницы, проблема все еще сохраняется. Я действительно сбит с толку, что это за проблема, поскольку каждая страница использует один и тот же код, и все же проблема присутствует для домашней страницы, а не для других.

MainView

    # import gi
# gi.require_version("Gtk", "3.0")
# from gi.repository import Gtk

""" tkinter imports """
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from tkinter import font

""" Page imports """
from Home import Home


#maybe these should go in the MainView class
""" Constants """
TITLE = "Program"
ABOUT_MESSAGE = "About"

debug = "debug_mode"
debug_mode = True

class MainView(tk.Frame):

    """ MainView attributes """
    my_font = None

    LANGUAGES = []
    # LANGUAGES = [Java, Python, HTML, Java, Python, HTML, Java, Python, HTML]
    pages = {}


    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)

        #tooth_fairy_protocol()
        my_font = font.Font(family = "Helvetica", size = 30, weight = "bold")

        #

        # create pages
        if debug_mode == True:
            page = Home(self, highlightbackground="pink", highlightcolor="pink", highlightthickness=10)
        else:
            page = Home(self)  

        self.pages[page.__class__.__name__] = page 
        self.show_home()

        self.setup_menu(parent, self.pages)


    def show_home(self):
        home_page = self.pages["Home"]
        home_page.pack_forget()
        for item in home_page.pages:
            home_page.pages[item].pack_forget()
        home_page.show() 

    def setup_menu(self, parent, pages):
        """ Setup menu bar """
        # setup the bar for the menu
        menubar = tk.Menu(self)

        # setup the button for the menu
        menubutton = tk.Menu(menubar, tearoff = 0)

        # setup file menu
        filemenu = tk.Menu(menubar, tearoff = 0)
        filemenu.add_command(label = "Home", command = lambda: self.show_home())
        filemenu.add_command(label = "Sandbox", command = lambda: messagebox.showinfo("Title", "Feature not implemented yet"))
        filemenu.add_command(label = "About", command = lambda: messagebox.showinfo("Title", ABOUT_MESSAGE))


        # add menu button to the menu bar
        menubar.add_cascade(label = "Menu", menu = menubutton)

        # add options to menu button
        menubutton.add_cascade(label = "File", menu = filemenu)
        menubutton.add_command(label = "Exit", command = self.quit)

        parent.config(menu = menubar)

def center(win):
    """ Centers the window on the screen. """
    win.update_idletasks()
    width = win.winfo_width()
    height = win.winfo_height()
    x = (win.winfo_screenwidth() // 2) - (width // 2)
    y = (win.winfo_screenheight() // 2) - (height // 2)
    win.geometry('{}x{}+{}+{}'.format(width, height, x, y))

def tooth_fairy_protocol():
    """
    Pillow Check
    install pillow on a windows pc
    """
    try:
        import PIP
    except ModuleNotFoundError:
        print("Installing PIP")

        import subprocess
        r = subprocess.run(["pip", "install", "Pillow"])
        # r = subprocess.run(["pip", "install", "gobject", "PyGObject"])

        if r.returncode != 0:
            print("PIL could not be installed")

if __name__ == "__main__":
    root = tk.Tk()

    main = MainView(root)
    main.pack(side="top", fill="both", expand=True)
    # main.grid(rowspan = 2, row = 1)

    # change aspect ratio of the program
    # root.wm_geometry("1280x720")
    sizex = 1280
    sizey = 720
    posx  = 100
    posy  = 100
    root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
    # center the program on screen
    center(root)

    # changes the window icon
    tk.Tk.iconbitmap(root, default = "icon.ico")

    # changes the title of the program
    tk.Tk.wm_title(root, TITLE)
    root.mainloop()

Страница

    """ tkinter imports """
import tkinter as tk

class Page(tk.Frame):
    parent = {}
    pages = {}
    canvas= None
    buttonframe = None
    debug = False

    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.parent = parent
        self.debug = True
        for item in args:
                if item == "debug_mode":
                    self.debug = True


        # Add page title
        label = tk.Label(self, text=self.__class__.__name__).pack(side = tk.TOP, fill=tk.X)

        # create a canvas object and a vertical scrollbar for scrolling it
        vscrollbar = tk.Scrollbar(self, orient = tk.VERTICAL)
        vscrollbar.pack(fill = tk.Y, side = tk.RIGHT, expand = False)
        canvas= self.canvas = tk.Canvas(self, bd = 0, highlightbackground="yellow", highlightcolor="yellow", highlightthickness=10, yscrollcommand = vscrollbar.set)
        canvas.pack(side = tk.LEFT, fill = tk.BOTH, expand = True)
        vscrollbar.config(command = canvas.yview)

         # reset the view
        canvas.xview_moveto(0)
        canvas.yview_moveto(0)

        # create a frame inside the canvas which will be scrolled with it
        if self.debug:
            self.buttonframe = tk.Frame(canvas, highlightbackground="red", highlightcolor="red", highlightthickness=10)
        else:
            self.buttonframe = tk.Frame(canvas)
        interior_id = canvas.create_window(0, 0, window = self.buttonframe, anchor = tk.NW)

        def _configure_interior(event):
            # update the scrollbars to match the size of the inner frame
            size = (self.buttonframe.winfo_reqwidth(), self.buttonframe.winfo_reqheight())
            canvas.config(scrollregion="0 0 %s %s" % size)
            if self.buttonframe.winfo_reqwidth() != canvas.winfo_width():
                # update the canvas's width to fit the inner frame
                canvas.config(width = self.buttonframe.winfo_reqwidth())
        self.buttonframe.bind('<Configure>', _configure_interior)

        def _configure_canvas(event):
            if self.buttonframe.winfo_reqwidth() != canvas.winfo_width():
                # update the inner frame's width to fill the canvas
                canvas.itemconfigure(interior_id, width = canvas.winfo_width())
        canvas.bind('<Configure>', _configure_canvas)

    def show(self):
        """ 
        Show the Page 
        """

        self.pack(side = "top", expand = True, fill = tk.BOTH)

Главная

    """ tkinter imports """
import tkinter as tk
from tkinter import ttk



""" Page imports """
from Page import Page


class Home(Page):
    """ 
    Home page of the program - this will have all language options on, each will link to their own page 
    """

    def __init__(self, parent, *args, **kwargs):
        Page.__init__(self, parent, *args, **kwargs)

    def show_page(self, language, pages):
        self.pack_forget()
        pages[language.__name__].show()

Если кто-нибудь может мне помочь с этим, это будет очень ценно.

Ответы [ 2 ]

0 голосов
/ 02 апреля 2019

Благодаря гораздо более скромному примеру figbeam Мне удалось найти проблему.

В вашем Page классе вы инициализируете кадр как

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

Здесь следует отметить, что вы не проходите parent. Следовательно, у рамки Page будет не MainView в качестве его родителя, а корневое окно. Из-за этого эти два кадра упакованы друг на друга, Page над MainView. Когда вы pack_forget Page и повторно pack его, он теперь помещается под рамкой MainView.

Решение состоит в том, чтобы дать инициализации кадра в классе Page parent, как

tk.Frame.__init__(self, parent, *args, **kwargs)
0 голосов
/ 01 апреля 2019

Это было сложно. Я не знаком с выбранным вами решением и не очень понимаю, как оно должно работать. Трудно найти проблему в действии, охватывающем несколько классов.

Просто, чтобы привести пример того, что может составлять минимальный пример, я включил некоторый код, который копирует поведение. Для активации просто нажмите Пробел вместо использования меню.

import tkinter as tk
from Home import Home

class MainView(tk.Frame):
    pages = {}

    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)

        page = Home(self, highlightbackground="pink",
                        highlightcolor="pink", highlightthickness=10)

        self.pages[page.__class__.__name__] = page 
        self.show_home()
        parent.bind('<space>', lambda event: self.show_home())

    def show_home(self):
        home_page = self.pages["Home"]
        home_page.pack_forget()

        for item in self.pages:
            self.pages[item].pack_forget()
        home_page.show() 

root = tk.Tk()
main = MainView(root)
main.pack(side="top", fill="both", expand=True)
root.wm_geometry("500x500+500+200")
root.mainloop()

Возможно, кто-то еще может использовать этот код, чтобы попытаться решить эту проблему. Я ничего не сделал для других классов.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...