在一个基本程序(启动器)上我有一个滚动条问题:它们使背景图片移动而不是forderground小部件(按钮)。
GUI构建如下:
0级(父=自我):画布(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()滚动条使Canvas“self.c”移动,但不是画布中的按钮。 经过大量的测试后,我仍然没有发现错误。 有人对这个问题有所了解吗?
谢谢!
On a basic program (launcher) I have a scrollbar problem : they make the background-picture move but not the forderground widgets (buttons).
The GUI is built as follow :
Level 0 (parent = self) : Canvas (self.c) as background with a picture + scrollbars Level 1 (parent = self.c) : Buttons + an invisible FrameHere is the code :
# -*-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()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?
Thanks!
最满意答案
对于要使用画布滚动的内容,它必须是画布的一部分。 这意味着您必须使用create_window而不是pack , place或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.
更多推荐
发布评论