0

I am trying to create a page which will have all the information for the rest of my application. Here is the rest of the code:

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

def setColourScheme(widget):
  try:
    widget.configure(bg="#251f1e")
    widget.configure(fg="white")
  except:
    pass

def autoPack(widget, Side, Row, Column, Xpad=5, Ypad=5, Fill=None, Expand=True, **kwargs):
  setColourScheme(widget)
  widget.pack(side=Side, fill=Fill, expand=Expand, padx=Xpad, pady=Ypad, **kwargs)
  try:
    widget.grid_rowconfigure(Row, weight=1)
    widget.grid_columnconfigure(Column, weight=1)
  except:
    pass

def createNavigationPane(self):
  self.navigationFrame = Frame(self)
  autoPack(self.navigationFrame, "bottom", 1, 0)
  autoPack(Button(self.navigationFrame, text="Settings", command=lambda: self.controller.show_frame()), "left", 0, 0, 100)
  autoPack(Button(self.navigationFrame, text="Trading page", command=lambda: self.controller.show_frame()), "left", 0, 1, 100)
  autoPack(Button(self.navigationFrame, text="Information/help", command=lambda: self.controller.show_frame()), "left", 0, 2, 100)
  autoPack(Button(self.navigationFrame, text="Analysis", command=lambda: self.controller.show_frame()), "left", 0, 3, 100)
  autoPack(Button(self.navigationFrame, text="Exit", command=lambda: self.controller.show_frame()), "left", 0, 4, 100)


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

    setColourScheme(self)

    Tk.wm_title(self, "information page")

    self.geometry("1920x1080")
    self.resizable(False,False)

    self.default = font.nametofont("TkDefaultFont")

    self.default.configure(family="productsans", size=20, weight="normal")

    container = Frame(self)
    autoPack(container, "top", 0, 0, 5, 5, "both")

    self.pages = {}

    frame = InformationPage(container, self)

    self.pages[InformationPage] = frame

    autoPack(frame, "top", 0, 0, 5, 5, "both")
  def show_frame(self):
    pass

class InformationPage(Frame):
  def __init__(self, parent, controller):
    Frame.__init__(self, parent)
    self.controller = controller

    setColourScheme(self)

    self.listOfData = LabelFrame(self)
    setColourScheme(self.listOfData)

    self.canvasForText = Canvas(self.listOfData, height=850, width=1850)
    self._autoPack(self.canvasForText, "left", None, None, 0, 0, "both")

    self.yscrollbar = ttk.Scrollbar(self.listOfData, orient="vertical", command=self.canvasForText.yview)
    self._autoPack(self.yscrollbar, "right", None, None, 0, 0, "y")

    self.canvasForText.configure(yscrollcommand=self.yscrollbar.set)

    self.canvasForText.bind('<Configure>', lambda e: self.canvasForText.configure(scrollregion = self.canvasForText.bbox('all')))

    self.textFrame = Frame(self.canvasForText)
    setColourScheme(self.textFrame)
    self.canvasForText.create_window((0,0), window=self.textFrame, anchor="nw")

    self._autoPack(self.listOfData, None, None, None, 5, 5, "both")
    
    createNavigationPane(self)

    self.loadInformation()

  def _autoPack(self, widget, Side, Row, Column, Xpad=0, Ypad=0, Fill=None, Expand=True, **kwargs):
    self._setColourScheme(widget)
    widget.pack(side=Side, fill=Fill, expand=Expand, padx=Xpad, pady=Ypad, **kwargs)
    try:
      widget.grid_rowconfigure(Row, weight=1)
      widget.grid_columnconfigure(Column, weight=1)
    except:
      pass

  def _setColourScheme(self, widget, bold="normal"):
    try:
      widget.configure(bg="#251f1e")
      widget.configure(fg="white")
    except:
      pass
    try:
      widget.configure(font=("productsans", 18, bold))
    except:
      pass

  def _autoGrid(self, widget, Row, Column, bold="normal", Xpad=0, Ypad=5):
    self._setColourScheme(widget, bold)
    widget.grid(row=Row, column=Column, padx=Xpad, pady=Ypad, sticky='w')

The below method in the InformationPage class is the one that adds the labels and text to the frame which is linked to the scrollbar. The issue I have is that the image isn't displayed in the frame, although there is a blank white box so it causes no errors.

  def loadInformation(self):
    lines = ["Background information",
" ",
" ",
"Key terminology",
" ",
"Market – a place where goods and services are sold",
" ",
"Shares – the smallest amount the ownership of a business can be divided into (represents units of stock)",
" ",
"Dividend – the sum of money paid regularly by a company to its shareholders out of its profits",
" ",
"Stocks – a share which entitles the holder to a fixed dividend (and takes priority over an ordinary share)",
" ",
"Currency – a system of money used in a country",
" ",
"Forex (Foreign Exchange) – this is the trading of one currency for another currency",
" ",
"Cryptocurrency – a digital currency in which transactions are verified and records maintained by a decentralised system using cryptography, rather than by a",
" ",
"                 centralised authority, which means that the values aren’t decided by outside factors and solely by the amounts of cryptocurrency used.",
" ",
" ",
"Candlestick Charts",
"",
"These are charts that display the change in the market value of a product through candles. These are generally used to make trading decision based on",
" ",
"regularly occurring patterns that help traders predict how the price of a product is likely to change in the short term.",
" ",
"image",]
    n = -1
    for line in lines:
      n += 1
      if line in [lines[0],lines[2]]:
        self._autoGrid(Label(self.textFrame, text=line, anchor='w'), n, 0, "bold")
      elif line == "image":
        canv = Label(self.textFrame)
        canv.grid(row=n, column=0, sticky='w')
        
        img = ImageTk.PhotoImage(Image.open("candleExplanation.png"))
        canv.configure(image=img, anchor='w')
      else:
        self._autoGrid(Label(self.textFrame, text=line, anchor='w'), n, 0)


app = window()
app.mainloop()

Sorry for the massive bundle of code, but I tried to look for any other posts on this and there wasn't any solutions. Also new to stackoverflow so may have made post too long - sorry again.

  • Try adding `canv.img = img` on the line after `img = ImageTk.PhotoImage(...`. [This question](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) may help. – Henry Feb 06 '22 at 17:17
  • Thanks, however it comes up with error: name 'img' is not defined. – Just a dude Feb 06 '22 at 23:07
  • You've just defined `img` on the previous line so this should not happen. You should have `img = ImageTk.PhotoImage(Image.open("candleExplanation.png"))` newline `canv.img = img` newline `canv.configure(image=img, anchor='w')` – Henry Feb 07 '22 at 01:24
  • Yep, thanks, it works now, it was just that I thought that the `image=img` didn't need to be in the label so I had removed it. Completely my mistake. Thanks again – Just a dude Feb 07 '22 at 13:33
  • No problem, glad I could help – Henry Feb 07 '22 at 13:41

0 Answers0