滚动条 - 使背景移动,但不是forderground [英] Scrollbar - make the background move but not the forderground

查看:157
本文介绍了滚动条 - 使背景移动,但不是forderground的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个基本的程序(启动器)我有一个滚动条问题:他们使背景图片移动,但不是forderground小部件(按钮)。

On a basic program (launcher) I have a scrollbar problem : they make the background-picture move but not the forderground widgets (buttons).

建立如下:


  • 级别0(parent = self):Canvas(self.c)作为背景图片+滚动条

  • 第1级(parent = self.c):按钮和不可见框架

# -*-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 des 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 - all other widgets but the scrollbars are in self.c
        self.c = Tkinter.Canvas(self, yscrollcommand=self.ascenseur_y.set, xscrollcommand=self.ascenseur_x.set,\
                                bg="white", highlightthicknes=0)
        self.apercu_logo="Logo.png"
        self.photo = ImageTk.PhotoImage(Image.open(self.apercu_logo))
        self.c.config(height=self.photo.height(), width=self.photo.width())
        self.image_x = self.photo.height()/2
        self.image_y = self.photo.width()/2
        self.item = self.c.create_image(self.image_x, self.image_y, anchor="center", image=self.photo)
        self.c.grid(row=0, column=0, sticky="news")
        self.c.bind('<Configure>', self.dimensionner)

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

        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.bind_all("<MouseWheel>", self._on_mousewheel_haupt)

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

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

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

    # Configuration rows/columns
        self.c.grid_rowconfigure(0, weight=1, pad=100, minsize=300)

        self.c.grid_columnconfigure(0, weight=1, pad=30, minsize=140)
        self.c.grid_columnconfigure(1, weight=1, pad=30, minsize=140)
        self.c.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.c.create_window(0, 0, window=self.fr)
        self.fr.update_idletasks()
        self.c.config(scrollregion=self.c.bbox("all"))
        # Make sure all widgets are updated
        self.update()




# Fonctions 

    def dimensionner(self, event):
        """ Management of the resizement of the window:
            Centers the background-picture
            Modifies the size of rows/columns
            Resizes the scrollbars """
        self.image_y = self.winfo_height()/2
        self.image_x = self.winfo_width()/2
        self.c.delete(self.item)
        self.item = self.c.create_image(self.image_x, self.image_y, anchor="center", image=self.photo)
        if self.winfo_height() < 300 and self.winfo_height() > 100:
            self.c.grid_rowconfigure(0, weight=1, pad=100, minsize=self.winfo_height())
        elif self.winfo_height() < 100 :
            self.c.grid_rowconfigure(0, weight=1, pad=100, minsize=98)
        else :
            self.c.grid_rowconfigure(0, weight=1, pad=100, minsize=300)

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

        if self.winfo_height() > 223 and self.winfo_width() > 420 :
            self.SR = self.c.bbox("all")
            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() < 223 and self.winfo_width() > 420 :
            self.SR = self.c.bbox("all")
            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() > 223 and self.winfo_width() < 420 :
            self.SR = self.c.bbox("all")
            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 :
            pass



    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()

滚动条使画布self.c移动,但不是在画布中的按钮。经过很多测试后,我还是没有发现错误。有人知道这个问题吗?

The scrollbars make the Canvas "self.c" move but not the buttons which are in the canvas. After a lot of tests I still don't find the mistake. Does someone have an idea about the problem?

谢谢!

推荐答案

对于使用canvas来滚动的东西,它必须是canvas的一部分。这意味着您必须使用 create_window 而不是 pack 放置 grid 将子窗口小部件添加到画布。

For something to scroll with the canvas it must be part of the canvas. That means you have to use create_window rather than pack, place or grid to add child widgets to a canvas.

这篇关于滚动条 - 使背景移动,但不是forderground的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆