0

I'm creating a dynamic container that can change between frames. In one of the frames I have a list of values that I need to scroll through because it is so long. However I cannot get the scrollbar to work with the canvas and frame set up.

i have tried using Listbox and this works but does not give me the control over the displays that I am looking for. I want to be able to configure the names of the tag and then to the right the value.

#!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()
  • _" I cannot get the scrollbar to work "_ - what does that mean? Why can't you? What does the code do, and how is it different from what you expect it to do? – Bryan Oakley Jun 27 '19 at 13:49
  • it means that the scrollbar will not scroll through the values as the list is too to display on the screen. the bar simply does not work. This code works: https://stackoverflow.com/questions/16188420/tkinter-scrollbar-for-frame I would expect my cool to work similarly – Sangin Koala Jun 27 '19 at 15:09
  • Your code isn't at all like the code in that question. – Bryan Oakley Jun 27 '19 at 17:28

2 Answers2

0

The canvas can't scroll things added to the canvas with grid. The most common solution is to add a frame to the canvas with create_window, and then adding the labels to the frame.

See Adding a scrollbar to a group of widgets in Tkinter

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

So i made a few changes thanks and got it working :) Bye using pack as you sugeted it works

#!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()