python - Scrollbar - make the background move but not the forderground -
on basic program (launcher) have scrollbar problem : make background-picture move not forderground widgets (buttons).
the gui built follow :
- level 0 (parent = self) : canvas (self.c) background picture + scrollbars
- level 1 (parent = self.c) : buttons + invisible frame
here code :
# -*-coding:utf-8 -*- __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 - other widgets scrollbars 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 bind mousewheel event 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 main window self.iconbitmap("icokag.ico") # options of main window # resizable self.resizable(true, true) # principal canvas , config of scrollbars self.c.create_window(0, 0, window=self.fr) self.fr.update_idletasks() self.c.config(scrollregion=self.c.bbox("all")) # make sure widgets updated self.update() # fonctions def dimensionner(self, event): """ management of resizement of window: centers background-picture modifies size of rows/columns resizes 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 , 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 , 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 , 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 , 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 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 build if __name__ == "__main__" : applauncher = interface(none) applauncher.title(u"programm-launcher - kämmerer ag") applauncher.mainloop()
the scrollbars make canvas "self.c" move not buttons in canvas. after lot of tests still don't find mistake. have idea problem?
thanks!
for scroll canvas must part of canvas. means have use create_window
rather pack
, place
or grid
add child widgets canvas.
Comments
Post a Comment