滚动使小部件变得不可见

时间:2013-07-26 13:16:39

标签: python widget tkinter scrollbar

在基本的启动程序(最近更正,请参阅Scrollbar - make the background move but not the forderground)上,我遇到以下问题:当我使用滚动条时按钮消失。实际上它们仍然存在,我可以点击按钮,但它们现在可见。如果鼠标光标在按钮位置上移动然后离开该区域,则该按钮会重新出现。 我认为问题是GUI的更新。这就是为什么我在程序中使用了很多更新(可能太多)但它仍然不起作用。

这是我的代码:

# -*-coding:Utf-8 -*-
from __future__ import unicode_literals

import Tkinter
import ImageTk
import Image



class Interface(Tkinter.Tk) :
    def __init__(self, parent) :
        Tkinter.Tk.__init__(self, parent)
        self.parent = parent
        self.initialize()

# Creation of the widgets
    def initialize(self) :
    # Fenetre principale
        self.minsize(437, 98)
        # Scrollbars working on principal Canvas self.c
        self.ascenseur_y = Tkinter.Scrollbar(self, orient=Tkinter.VERTICAL)
        self.ascenseur_x = Tkinter.Scrollbar(self, orient=Tkinter.HORIZONTAL)
        self.ascenseur_y.grid(row=0, column=1, sticky="ns")
        self.ascenseur_x.grid(row=1, column=0, sticky="ew")

        # Canvas self.c - Frame self.fr and label self.bl are in self.c
        self.c = Tkinter.Canvas(self, yscrollcommand=self.ascenseur_y.set, xscrollcommand=self.ascenseur_x.set, bg="white", highlightthicknes=0)
        self.c.grid(row=0, column=0, sticky="news")
        self.c.bind('<Configure>', self.dimensionner)

        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

        self.ascenseur_x.config(command=self.c.xview)
        self.ascenseur_y.config(command=self.c.yview)

        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

    # Invisible Frame to bind Mousewheel event with vertical scrollbar
        self.fr = Tkinter.Frame(self.c)
        self.fr.pack(expand=True, fill="both")
        self.fr.bind_all("<MouseWheel>", self._on_mousewheel_haupt)

    # Def Label with background-picture. all other widgets but the scrollbars are in self.bl
        self.apercu_logo="Logo.png"
        self.photo = ImageTk.PhotoImage(Image.open(self.apercu_logo))
        self.bl = Tkinter.Label(self.c, image=self.photo, bg="white")

        self.image_y = self.winfo_height()/2
        self.image_x = self.winfo_width()/2
        self.c.create_window(450/2, 300/2, window=self.bl, height=500, width=800)
        self.bl.update_idletasks()

    # Button "start pdf2pptx"
        self.bouton_pdf2pptx = Tkinter.Button(self.bl, width=13, text=u"Pdf2pptx", command=self.ButtonPdf2pptx, anchor="center", cursor="hand2", padx=0)
        self.bouton_pdf2pptx.grid(column=0, row=0, padx=10)
        self.bouton_pdf2pptx.bind("<Return>", self.EnterPdf2pptx)
        self.bouton_pdf2pptx.focus_set()

    # Button "start xls2inp"
        self.bouton_xls2inp = Tkinter.Button(self.bl, width=13, text=u"xls2inp", command=self.ButtonXls2inp, anchor="center", cursor="hand2", padx=0)
        self.bouton_xls2inp.grid(column=1, row=0, padx=10)
        self.bouton_xls2inp.bind("<Return>", self.EnterXls2inp)

    # Button "start Zeichen ersetzer"
        self.bouton_ZeichenErsetzer = Tkinter.Button(self.bl, width=13, text=u"Zeichen ersetzer", command=self.ButtonZeichenErsetzer, anchor="center", cursor="hand2", padx=0)
        self.bouton_ZeichenErsetzer.grid(column=2, row=0, padx=10)
        self.bouton_ZeichenErsetzer.bind("<Return>", self.EnterZeichenErsetzer)


    # Configuration rows/columns - in self.bl

        self.bl.grid_rowconfigure(0, weight=1, pad=100, minsize=50)

        self.bl.grid_columnconfigure(0, weight=1, pad=30, minsize=140)
        self.bl.grid_columnconfigure(1, weight=1, pad=30, minsize=140)
        self.bl.grid_columnconfigure(2, weight=1, pad=30, minsize=140)

    # Logo of the main window
        self.iconbitmap("IcoKAG.ico")

# Options of the main window
        # Resizable
        self.resizable(True, True)
        # Principal Canvas and config of the scrollbars
        self.image_y = self.winfo_height()/2
        self.image_x = self.winfo_width()/2
        self.c.create_window(self.image_x, self.image_y, window=self.fr)
        self.fr.update_idletasks()

        # Min size of main window
        self.minsize(200, 100)

        # Size of main window at the opening
        self.geometry("500x300")

        if self.winfo_width() < 437 :
            self.bl.grid_columnconfigure(0, weight=1, pad=30, minsize=self.winfo_width()/3)
            self.bl.grid_columnconfigure(1, weight=1, pad=30, minsize=self.winfo_width()/3)
            self.bl.grid_columnconfigure(2, weight=1, pad=30, minsize=self.winfo_width()/3)
        else :
            self.bl.grid_columnconfigure(0, weight=1, pad=30, minsize=140)
            self.bl.grid_columnconfigure(1, weight=1, pad=30, minsize=140)
            self.bl.grid_columnconfigure(2, weight=1, pad=30, minsize=140)

        if self.winfo_height() >= 300 and self.winfo_width() >= 500 :
            self.SR = (0, 0, self.photo.width(), self.photo.height())
            self.SRL = list(self.SR)
            self.SRL[2] = self.winfo_width() - 16
            self.SRL[3] = self.winfo_height() - 16
            self.c.config(scrollregion=tuple(self.SRL))
            self.update()
        elif self.winfo_height() < 300 and self.winfo_width() >= 500 :
            self.SR = (0, 0, self.photo.width(), self.photo.height())
            self.SRL = list(self.SR)
            self.SRL[2] = self.winfo_width() - 16
            self.SRL[3] -= 16
            self.c.config(scrollregion=tuple(self.SRL))
            self.update()
        elif self.winfo_height() >= 300 and self.winfo_width() < 500 :
            self.SR = (0, 0, self.photo.width(), self.photo.height())
            self.SRL = list(self.SR)
            self.SRL[2] -= 16
            self.SRL[3] = self.winfo_height() - 16
            self.c.config(scrollregion=tuple(self.SRL))
            self.update()
        else :
            self.SR = (0, 0, self.photo.width(), self.photo.height())
            self.SRL = list(self.SR)
            self.SRL[2] -= 16
            self.SRL[3] -= 16
            self.c.config(scrollregion=tuple(self.SRL))
            self.update()
        # Make sure all widgets are updated
        self.update()
        self.bl.update_idletasks()




# Fonctions 

    def dimensionner(self, event):
        """ Gestion du redimentionnement de la taille de la fenêtre :
            Repositionne l'image de fond
            Modifie la taille des lignes/colonnes
            Regle les scrollbars """
    # Scrollbars options
        if self.winfo_width() < 437 :
            self.bl.grid_columnconfigure(0, weight=1, pad=30, minsize=self.winfo_width()/3)
            self.bl.grid_columnconfigure(1, weight=1, pad=30, minsize=self.winfo_width()/3)
            self.bl.grid_columnconfigure(2, weight=1, pad=30, minsize=self.winfo_width()/3)
            self.update()
            self.c.update_idletasks()
        else :
            self.bl.grid_columnconfigure(0, weight=1, pad=30, minsize=140)
            self.bl.grid_columnconfigure(1, weight=1, pad=30, minsize=140)
            self.bl.grid_columnconfigure(2, weight=1, pad=30, minsize=140)
            self.update()
            self.c.update_idletasks()

        if self.winfo_height() >= 300 and self.winfo_width() >= 500 :
            self.SR = (0, 0, self.photo.width(), self.photo.height())
            self.SRL = list(self.SR)
            self.SRL[2] = self.winfo_width() - 16
            self.SRL[3] = self.winfo_height() - 16
            self.c.config(scrollregion=tuple(self.SRL))
            self.update()
            self.c.update_idletasks()
        elif self.winfo_height() < 300 and self.winfo_width() >= 500 :
            self.SR = (0, 0, self.photo.width(), self.photo.height())
            self.SRL = list(self.SR)
            self.SRL[2] = self.winfo_width() - 16
            self.SRL[3] = 300 - 16
            self.c.config(scrollregion=tuple(self.SRL))
            self.update()
            self.c.update_idletasks()
        elif self.winfo_height() >= 300 and self.winfo_width() < 500 :
            self.SR = (0, 0, self.photo.width(), self.photo.height())
            self.SRL = list(self.SR)
            self.SRL[2] = 500 - 16
            self.SRL[3] = self.winfo_height() - 16
            self.c.config(scrollregion=tuple(self.SRL))
            self.update()
            self.c.update_idletasks()
        else :
            self.c.config(scrollregion=(0, 0, 484, 284))
            self.update()
            self.c.update_idletasks()

    # Center the Background-label
        if self.winfo_height() >= 300 and self.winfo_width() >= 500 :
            self.image_y = self.winfo_height()/2
            self.image_x = self.winfo_width()/2
            self.c.create_window(self.image_x, self.image_y, window=self.bl, height=self.winfo_height(), width=self.winfo_width())
            self.update()
            self.c.update_idletasks()
        elif self.winfo_height() < 300 and self.winfo_width() >= 500 :
            self.image_y = 300/2
            self.image_x = self.winfo_width()/2
            self.c.create_window(self.image_x, self.image_y, window=self.bl, height=300, width=self.winfo_width())
            self.update()
            self.c.update_idletasks()
        elif self.winfo_height() >= 300 and self.winfo_width() < 500 :
            self.image_y = self.winfo_height()/2
            self.image_x = 500/2
            self.c.create_window(self.image_x, self.image_y, window=self.bl, height=self.winfo_height(), width=500)
            self.update()
            self.c.update_idletasks()
        else :
            self.image_y = 300/2
            self.image_x = 500/2
            self.c.create_window(self.image_x, self.image_y, window=self.bl, height=300, width=500)
            self.update()
            self.c.update_idletasks()



    def _on_mousewheel_haupt(self, event):
        """ Bind mousewheel to y-scrollbar """
        self.c.yview_scroll(-1*(event.delta/120), "units")

    def ButtonPdf2pptx(self) :
        pass

    def EnterPdf2pptx(self, event) :
        self.ButtonPdf2pptx()

    def ButtonXls2inp(self) :
        pass

    def EnterXls2inp(self, event) :
        self.ButtonXls2inp()

    def ButtonZeichenErsetzer(self) :
        pass

    def EnterZeichenErsetzer(self, event) :
        self.ButtonZeichenErsetzer()



# Main window is build
if __name__ == "__main__" :
    appLauncher = Interface(None)
    appLauncher.title(u"Programm-launcher - Kämmerer AG")
    appLauncher.mainloop()

有人有想法吗?

1 个答案:

答案 0 :(得分:0)

我无法复制您的结果。我看到三个按钮,它们永远不会消失。

您使用太多更新是正确的。您不应该在update中对update_idletaskdimensionner进行任何调用 - 该功能仅作为事件的结果调用,因此UI应该已经是最新的函数被调用。

此外,你有很多代码没有做你认为它正在做的事情。例如:

self.bl.grid_rowconfigure(0, weight=1, pad=100, minsize=50)

这只会影响放在按钮内的按钮的子项。您的代码没有在按钮内放置任何小部件(这非常不寻常)。当您调用grid_rowconfiguregrid_columnconfigure时,您正在配置小部件中的行和列,而不是包含小部件的行和列。