-Thomas杨- 2023-02-09 08:52 采纳率: 50%
浏览 64
已结题

Tkinter助手生成的GUI怎么才能绑定滚动条

我不太懂这种初始化GUI的方式, 尝试过在容器内创建滚动条 但是绑定的时候会因为对象属性问题报错
生成的源代码如下


from tkinter import *
from tkinter.ttk import *


class WinGUI(Tk):
    def __init__(self):
        super().__init__()
        self.__win()
        self.tk_tabs_ldwd7cby = Frame_ldwd7cby(self)

    def __win(self):
        self.title("Tkinter布局助手")
        # 设置窗口大小、居中
        width = 600
        height = 500
        screenwidth = self.winfo_screenwidth()
        screenheight = self.winfo_screenheight()
        geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        self.geometry(geometry)
        self.resizable(width=False, height=False)


class Frame_ldwd7cby(Notebook):
    def __init__(self, parent):
        super().__init__(parent)
        self.__frame()

    def __frame(self):
        self.tk_tabs_ldwd7cby_0 = Frame_ldwd7cby_0(self)
        self.add(self.tk_tabs_ldwd7cby_0, text="选项卡1")

        self.tk_tabs_ldwd7cby_1 = Frame_ldwd7cby_1(self)
        self.add(self.tk_tabs_ldwd7cby_1, text="选项卡2")

        self.place(x=0, y=0, width=599, height=490)


class Frame_ldwd7cby_0(Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.__frame()
        self.tk_list_box_ldwd7ol0 = self.__tk_list_box()

    def __frame(self):
        self.place(x=0, y=0, width=599, height=490)

    def __tk_list_box(self):
        lb = Listbox(self)
        lb.insert(END, "列表框")
        lb.insert(END, "Python")
        lb.insert(END, "Tkinter Helper")
        lb.place(x=30, y=20, width=355, height=326)

        return lb




class Frame_ldwd7cby_1(Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.__frame()

    def __frame(self):
        self.place(x=0, y=0, width=599, height=490)


class Win(WinGUI):
    def __init__(self):
        super().__init__()
        self.__event_bind()

    def __event_bind(self):
        pass


if __name__ == "__main__":
    win = Win()
    win.mainloop()


下面是我的插入方法 ,提示 'Frame_ldwd7cby_0' object has no attribute 'scroll_y1'


from tkinter import *
from tkinter.ttk import *


class WinGUI(Tk):
    def __init__(self):
        super().__init__()
        self.__win()
        self.tk_tabs_ldwd7cby = Frame_ldwd7cby(self)

    def __win(self):
        self.title("Tkinter布局助手")
        # 设置窗口大小、居中
        width = 600
        height = 500
        screenwidth = self.winfo_screenwidth()
        screenheight = self.winfo_screenheight()
        geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        self.geometry(geometry)
        self.resizable(width=False, height=False)


class Frame_ldwd7cby(Notebook):
    def __init__(self, parent):
        super().__init__(parent)
        self.__frame()

    def __frame(self):
        self.tk_tabs_ldwd7cby_0 = Frame_ldwd7cby_0(self)
        self.add(self.tk_tabs_ldwd7cby_0, text="选项卡1")

        self.tk_tabs_ldwd7cby_1 = Frame_ldwd7cby_1(self)
        self.add(self.tk_tabs_ldwd7cby_1, text="选项卡2")

        self.place(x=0, y=0, width=599, height=490)


class Frame_ldwd7cby_0(Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.__frame()
        self.tk_list_box_l = self.__tk_list_box()
        self.scroll_y1 = self.__scrollbar_y()

    def __frame(self):
        self.place(x=0, y=0, width=599, height=490)

    def __tk_list_box(self):
        lb = Listbox(self,yscrollcommand=self.scroll_y1.set)
        lb.insert(END, "列表框")
        lb.insert(END, "Python")
        lb.insert(END, "Tkinter Helper")
        lb.place(x=30, y=20, width=355, height=326)
        return lb

    def __scrollbar_y(self):
        scly = Scrollbar(self)
        scly.place(x=370, y=20 , width=20, height=326)
        scly.config(command= self.tk_list_box_l.yview)

        return scly



class Frame_ldwd7cby_1(Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.__frame()

    def __frame(self):
        self.place(x=0, y=0, width=599, height=490)


class Win(WinGUI):
    def __init__(self):
        super().__init__()
        self.__event_bind()

    def __event_bind(self):
        pass


if __name__ == "__main__":
    win = Win()
    win.mainloop()


请问怎么样才能正确绑定滚动条

  • 写回答

4条回答 默认 最新

  • 北海 2023-02-09 09:08
    关注

    我给一个简单的示例,你一看就知道了:

    import tkinter as tk
    
    root = tk.Tk()
    
    listbox = tk.Listbox(root)
    scrollbar = tk.Scrollbar(root, orient="vertical", command=listbox.yview)
    listbox.configure(yscrollcommand=scrollbar.set)
    
    listbox.pack(side="left", fill="both", expand=True)
    scrollbar.pack(side="right", fill="y")
    
    for i in range(1000):
        listbox.insert("end", str(i))
    
    root.mainloop()
    

    如果是水平滚动条,就设置xscrollcommand属性

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 2月17日
  • 已采纳回答 2月9日
  • 修改了问题 2月9日
  • 创建了问题 2月9日

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c