将价值传递给tkinter类

时间:2020-06-29 18:31:06

标签: python python-3.x tkinter

这是我实际程序的一部分。我正在尝试编写将在循环上创建几个Button的代码。 我的问题是我正在将值传递给tkinter类,但它没有打印正确的值,而是仅打印“。”。 我想在以下代码中将“章” 从PageOne类传递给“ ChapterOne类” ,该命令不起作用。我在课堂上没有太多经验。非常感谢您的帮助。

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

0 个答案:

没有答案