1

This is a part of my actual program. I am trying to write a code that will create few Buttons on loop. My problem is I am passing a value to a tkinter class but it is not printing the right value instead just prints a ".". I want to pass "chap" from class PageOne to "class ChapterOne" in the below code it isn't working. I don't have much experience in classes. A help here will be much appreciated.

import tkinter as tk
from PIL import ImageTk, Image
from os import listdir
import yaml

LARGE_FONT = ("Verdana", 12)
grey = "#808080"
offwhite = "#e3e3e3"
hintwa = False
x = ''

class MainBot(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)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        
        self.frames = {}

        for page in (StartPage, PageOne, ChapterOne):
            frame = page(container, self)
            print (container)
            self.frames[page] = frame
            frame.grid(row = 0, column = 0, sticky = 'nsew')

        self.show_frame(StartPage)

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

    def yaml_loader(self, filepath):
        with open (filepath, "r") as fileread:
            self.data = yaml.load(fileread)
            return self.data

class StartPage(tk.Frame):

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

        label = tk.Label(self, background = offwhite, text= "Start Page", font = LARGE_FONT)
        label.pack(pady=10, padx=10)

        button_start = tk.Button(self, text = 'NEXT', font = ("default", 15, "bold"), bg='orange', fg = 'white', border=2, height = 2, width = 8, command=lambda: controller.show_frame(PageOne))
        button_start.pack()
        button_start.place(x=650, y=500)

class PageOne(tk.Frame):

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

        index_label = tk.Label(self, text = "~~~~~INDEX~~~~~", font = ("Comicsans", 24, "bold"), background = offwhite)
        index_label.pack()
        index_label.place(x=200, y=50)

        onlyfiles = ['chapter-1.yaml']
        for yfiles in onlyfiles:
            chap = (yfiles.split("."))[0].split("-")[1]
            print (chap)
            button_index_one = tk.Button(self, text='Chapter ' + str(chap), font=("default", 14, "bold"), bg='white',
                                         fg='black', border=1, height=2, width=12,
                                         command=lambda: controller.show_frame(ChapterOne(self, chap)))
            button_index_one.pack(pady=30, padx=0)

class ChapterOne(tk.Frame):
    def __init__(self, parent, chap):
        tk.Frame.__init__(self, parent)
        print (chap)

app = MainBot()
app.geometry("800x600")
app.mainloop()
vivs
  • 113
  • 1
  • 1
  • 8
  • 1
    Does this answer your question? https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference – Thingamabobs Jun 30 '20 at 11:19
  • Thank you @Atlas435 for the link. I realized I shouldn't be using "command=lambda: controller.show_frame". – vivs Jul 01 '20 at 11:57

0 Answers0