前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Tkinter 滚动条使用导致按钮不可见

Tkinter 滚动条使用导致按钮不可见

原创
作者头像
华科云商小徐
发布2025-01-09 10:09:58
发布2025-01-09 10:09:58
7000
代码可运行
举报
文章被收录于专栏:小徐学爬虫小徐学爬虫
运行总次数:0
代码可运行

在使用 Tkinter 时,滚动条的配置不当可能导致窗口中某些组件(如按钮)变得不可见。这通常是因为滚动区域的布局没有正确处理,或其他组件未被添加到滚动区域之外的布局中。

问题背景

在使用 Tkinter 库创建了一个带有滚动条的基本启动程序时,当使用滚动条时,按钮会消失。虽然按钮仍然存在并可以点击,但它们不可见。当鼠标光标移至按钮位置然后离开该区域时,按钮会重新出现。这个问题似乎与 GUI 的更新有关。

解决方案

  1. 首先,确保所有小部件都在 self.bl 中,包括滚动条。这将确保它们受到滚动操作的影响。
  2. 其次,使用 update_idletasks()update() 方法确保所有小部件在调整窗口大小时正确更新。
  3. 第三,使用 config() 方法正确设置滚动区域。这将确保滚动条仅影响所需的区域,而不会影响其他小部件。
  4. 最后,使用 bind() 方法将鼠标滚轮事件绑定到 y 滚动条。这将允许用户使用鼠标滚轮滚动窗口。

以下是用 Python 修复此问题的代码示例:

代码语言:javascript
代码运行次数:0
运行
复制
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", highlightthickness=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()
            self.bl.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] -= 16
            self.c.config(scrollregion=tuple(self.SRL))
            self.update()
            self.bl.update_idletasks()
        elif self.winfo_height() >= 300 and self.winfo_width() < 500 :
            self.SR = (0,

以上方法可以解决 Tkinter 滚动条导致按钮不可见的问题。如果仍有问题,请提供具体代码或细节,我可以进一步帮助您优化!

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档