Tkinter滚动条,用于框架中的框架

时间:2019-06-27 13:18:14

标签: python-3.x tkinter scrollbar

我正在创建一个可以在不同帧之间更改的动态容器。在其中一个框架中,我有一个需要滚动的值列表,因为它很长。但是,我无法让滚动条与画布和框架设置一起使用。

我尝试使用列表框,但是可以,但是无法控制我要查找的显示。我希望能够配置标签的名称,然后在值的右侧。

#!Python
import matplotlib
matplotlib.use("TkAgg")

from matplotlib import style

import tkinter as tk
from tkinter import *

from YahooParser import Yahoo_Parser

TITLE_FONT = ("Helvetica", 10, "bold")
LARG_FONT = ("Helvetica", 12)
NORM_FONT = ("Helvetica", 10)
AIR_FONT = ("Arial", 10)
SMALL_FONT = ("Helvetica", 8)
style.use("ggplot")
#style.use("ggplot")

Gray = "#%02x%02x%02x" % (85, 85, 85)
Wight ="#%02x%02x%02x" % (220, 220, 220)


class Midas_Screen(tk.Tk):

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

        # Title
        tk.Tk.wm_title(self, "Midas Program")
        tk.Tk.geometry(self,"500x400")
        #tk.Tk.configure(self, background='black')

        # This set the seting for the container
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        # Makes all the frames and stores them
        for F in (HomePage,):

            # You pass the container to your page function. this makes the frame
            frame = F(container, self)
            # this aligns the frame "nsew" = North, South etc
            frame.grid(row=0, column=0, sticky="nsew")
            # This uses the function as a key in the dic
            self.frames[F] = frame

        self.show_frame(HomePage)


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


class HomePage(tk.Frame):

    def __init__(self, perent, controller):

        self.yp = Yahoo_Parser()
        self.names = []
        self.values = {}

        for tag in self.yp.values_sum:
            self.names.append(tag[0])
        for tag in self.yp.values_sta:
            self.names.append(tag[0])
        for tag in self.names:
            self.values[tag]='0'

        tk.Frame.__init__(self, perent)

        frame = Frame(perent)
        frame.grid(row=1, column=1)

        canvas = Canvas(frame)
        canvas.configure(scrollregion=(0,0,500,500), width=200, height=200)

        myscrollbar = Scrollbar(frame, orient="vertical")
        myscrollbar.grid(row=1, column=2, sticky="ns")
        myscrollbar.config(command=canvas.yview)

        canvas.config(yscrollcommand=myscrollbar.set)

        R = 1
        for key in self.values:
            label = tk.Label(canvas, text=key + ':', font=AIR_FONT, bg=Gray, fg=Wight, borderwidth=0,
                             relief="solid")
            value = tk.Label(canvas, text=self.values[key], font=AIR_FONT, bg=Gray, fg=Wight, borderwidth=0,
                             relief="solid")

            label.grid(row=R, column=1, sticky="nsew")
            value.grid(row=R, column=2, sticky="nsew")
            R += 1

        canvas.grid(row=1, column=1)


app = Midas_Screen()
app.mainloop()

2 个答案:

答案 0 :(得分:0)

画布无法滚动使用grid添加到画布的内容。最常见的解决方案是使用create_window将一个框架添加到画布,然后将标签添加到该框架。

请参见Adding a scrollbar to a group of widgets in Tkinter

答案 1 :(得分:0)

所以我做了一些更改,谢谢,并使其正常工作:) 再见了,请打包使用

#!Python
import matplotlib
matplotlib.use("TkAgg")

from matplotlib import style

import tkinter as tk
from tkinter import *

from YahooParser import Yahoo_Parser

TITLE_FONT = ("Helvetica", 10, "bold")
LARG_FONT = ("Helvetica", 12)
NORM_FONT = ("Helvetica", 10)
AIR_FONT = ("Arial", 10)
SMALL_FONT = ("Helvetica", 8)
style.use("ggplot")
#style.use("ggplot")

Gray = "#%02x%02x%02x" % (85, 85, 85)
Wight ="#%02x%02x%02x" % (220, 220, 220)


class Midas_Screen(tk.Tk):

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

        # Title
        tk.Tk.wm_title(self, "Midas Program")
        tk.Tk.geometry(self,"500x400")
        #tk.Tk.configure(self, background='black')

        # This set the seting for the container
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        # Makes all the frames and stores them
        for F in (HomePage,):

            # You pass the container to your page function. this makes the frame
            frame = F(container, self)
            # this aligns the frame "nsew" = North, South etc
            frame.grid(row=1, column=1, sticky="nsew")
            # This uses the function as a key in the dic
            self.frames[F] = frame

        self.show_frame(HomePage)


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


class HomePage(tk.Frame):

    def __init__(self, perent, controller):

        self.values = {}

        for tag in range(20):
            self.values['Text'+str(tag)+':']='0'

        tk.Frame.__init__(self, perent)

        def myfunction(event):
            canvas.configure(scrollregion=canvas.bbox("all"), width=200, height=200)

        canvas = Canvas(perent)
        frame = Frame(canvas)

        myscrollbar = Scrollbar(frame, orient="vertical", command=canvas.yview)
        canvas.config(yscrollcommand=myscrollbar.set)

        myscrollbar.pack(side=RIGHT, fill=Y)# sticky="ns")
        canvas.grid(row=0, column=0, sticky="nw")
        #canvas.pack(side=LEFT)
        canvas.create_window((0, 0), window=frame, anchor='nw')


        R = 1
        for key in self.values:
            row = Frame(frame)
            label = tk.Label(row, text=key + ':', font=AIR_FONT, bg=Gray, fg=Wight, borderwidth=0,
                             relief="solid")
            value = tk.Label(row, text=self.values[key], font=AIR_FONT, bg=Gray, fg=Wight, borderwidth=0,
                             relief="solid")

            label.pack(side=LEFT, fill=X)
            value.pack(side=LEFT,fill=X)
            row.pack(side=TOP, fill=X)
            R += 1

        frame.bind("<Configure>", myfunction)


app = Midas_Screen()
app.mainloop()