在一个基本程序(启动器)上,我有一个滚动条问题:它们使背景图片移动,而不是前景小部件(按钮)。
GUI的构建如下:
- Level 0 (parent = self) : 画布 (self.c) 作为背景,带有图片 + 滚动条
- 级别 1(父级 = 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”移动,但不是画布中的按钮。经过大量测试,我仍然没有发现错误。有人对这个问题有想法吗?
谢谢!