首页 > 解决方案 > 我似乎找不到将滚动条添加到我的 tkinter 元素周期表的方法

问题描述

import tkinter as tk
from tkinter import *
from tkinter import ttk
from PIL import Image,ImageTk

class App(tk.Frame):
    def __init__(self, parent,*args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent
        self.Frame = ttk.Frame(self.parent)
        tk.Text.__init__(self, self.Frame, width=1, height=1)

        self.winfo_toplevel().title("Periodic Table of the Elements")
        self.topLabel = tk.Label(self, text="Click the element", font=20)
        self.topLabel.grid(row=0, column=0, columnspan=18)
        self.__initWidget()



        self.infoLine = tk.Label(self, text="", justify='left')
        self.infoLine.grid(row=24, column=0, columnspan=10, rowspan=4)

    #self.mainloop() #Comment or remove it
    def scro(self,parent, *args,**kwargs):
        self._y_scrollbar = tk.Scrollbar(self, orient=tk.VERTICAL)
        
        self._text_widget = tk.Text(self, yscrollcommand=self._y_scrollbar.set, *args, **kwargs)
        self._text_widget.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
    
    # Replaces Label at the top with the name of whichever element tk.Button was pressed
    def name(self, text): 
        self.topLabel.config(text=text)
    # Displays information on the element of whichever element tk.Button was pressed
    def info(self, text): 
        self.infoLine.config(text=text)
    def scro(self):
        root = Tk()
        scrollbar = Scrollbar(root)
        scrollbar.pack( side = RIGHT, fill = Y )

        mylist.pack( side = LEFT, fill = BOTH )
        scrollbar.config( command = mylist.yview )
    def __initWidget(self): 
        self.Frame.grid(sticky="NSEW")
        self.ScrollbarY = ttk.Scrollbar(self.Frame, orient="vertical",command=self.yview)
        self.configure(yscrollcommand=self.ScrollbarY.set)
        self.grid(column=0, row=0, sticky="NSEW")
        self.ScrollbarY.grid(column=1, row=0, sticky="NS")
        self.Frame.columnconfigure(0, weight=1)
        self.Frame.rowconfigure(0, weight=1)

# Creates an instance of 'app' class
def main():
    root = tk.Toplevel()
    a = App(root) 
    a.grid(row=0, column=0, sticky='')
    a.mainloop()

# runs main function
if __name__ == "__main__":
    main()

我如何在其中添加滚动条,我已经删除了一些代码,主要是添加元素的循环,其他一切都还在。我尝试了各种方法,但似乎无法使其正常工作。我尝试将其打包在 def init中,但这不起作用,因为不能将网格和打包一起使用,我还尝试制作一个函数,它也显示在这里,但这不起作用。

标签: pythontkintergridscrollbar

解决方案


推荐阅读