Не могу прокрутить холст Tkinter - PullRequest
0 голосов
/ 23 апреля 2019

Я пытаюсь создать рамку (класс page1), чтобы заполнить метками для каждой строки CSV.Если строк слишком много, я бы хотел, чтобы пользователи могли просматривать их все.

Приложение запускается в кадре, для которого не нужна полоса прокрутки (class StartPage)

class SeaofBTCapp(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        container.pack(side="top",fill="both", expand = True)
        self.frames = {}

        page_list = [StartPage,page1]
        for F in page_list:
            frame = F(container, self, *args)
            self.frames[F] = frame
            frame.grid(row=0, column = 0, sticky="nsew")

        self.show_frame(StartPage)
        canvas = tk.Canvas(self, height = 700,width = 800)
        canvas.pack()

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

class StartPage (tk.Frame):

    def __init__ (self,parent,controller):
        tk.Frame.__init__(self,parent)

        button = tk.Button(self, text="visit page1", command=lambda: controller.show_frame(page1))
        button.pack()

class page1 (tk.Frame):

    def __init__(self,parent,controller):
        tk.Frame.__init__(self, parent)
        canvas = tk.Canvas(self, height = 700,width = 800)
        canvas.pack()

        csv = pd.read_csv('guitest_sheet/pd_guitest.csv', engine='python')
        csv_df = pd.DataFrame(csv)
        count = 0
        relx = .5
        height = 45
        for row in csv.iterrows() : 
            frame_name = "frame" + str(count)
            frame_name = tk.Frame(canvas, bg="black", bd=5)
            frame_name.place(width=400, height=height, relx=relx, y=((count+1)*height)+10,anchor='center')
            for x in row :
                if type(x) != int: 
                    label = tk.Label(frame_name, text="label")
                    label.place(width=100, anchor="n",relx=.5, rely=0)
            count+=1

        scroll_x = tk.Scrollbar(parent, orient="horizontal", command=canvas.xview)
        scroll_x.grid(row=1, column=0, sticky="ew")

        scroll_y = tk.Scrollbar(parent, orient="vertical", command=canvas.yview)
        scroll_y.grid(row=0, column=1, sticky="ns")

        canvas.configure(yscrollcommand=scroll_y.set, xscrollcommand=scroll_x.set,scrollregion=canvas.bbox('all'))

app = SeaofBTCapp()
app.mainloop()

1 Ответ

0 голосов
/ 23 апреля 2019

Этот код работает для меня, но после всех изменений я не помню, что я изменил.

import tkinter as tk
import pandas as pd

class SeaofBTCapp(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(fill="both", expand=True)

        container.columnconfigure(0, weight=1)
        container.rowconfigure(0, weight=1)

        self.frames = {}

        page_list = [StartPage, Page1]
        for F in page_list:
            frame = F(container, self, *args)
            self.frames[F] = frame
            frame.grid(row=0, column = 0, sticky="news")

        self.show_frame(StartPage)


    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class StartPage (tk.Frame):

    def __init__ (self,parent,controller):
        tk.Frame.__init__(self,parent)

        button = tk.Button(self, text="visit page1", command=lambda: controller.show_frame(Page1))
        button.pack()

class Page1(tk.Frame):

    def __init__(self,parent,controller):
        tk.Frame.__init__(self, parent)
        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)

        self.canvas = tk.Canvas(self)
        self.canvas.grid(row=0, column=0, sticky='news')

        scroll_x = tk.Scrollbar(self, orient="horizontal", command=self.canvas.xview)
        scroll_x.grid(row=1, column=0, sticky="ew")

        scroll_y = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
        scroll_y.grid(row=0, column=1, sticky="ns")

        self.canvas.configure(yscrollcommand=scroll_y.set)
        self.canvas.configure(xscrollcommand=scroll_x.set)

        inner_frame = tk.Frame(self.canvas)
        self.canvas.create_window((0, 0), window=inner_frame, anchor='nw')

        csv = pd.read_csv('test.csv', engine='python')
        for row in csv.iterrows() : 
            frame_name = tk.Frame(inner_frame)
            frame_name.pack()

            for x in row:
                if not isinstance(x, int): 
                    label = tk.Label(frame_name, text="label")
                    label.pack()

        self.canvas.bind('<Configure>', self.inner_resize)

    def inner_resize(self, event):
        self.canvas.configure(scrollregion=self.canvas.bbox('all'))


app = SeaofBTCapp()
app.mainloop()
...