在另一帧上显示信息? (tkinter python)

时间:2016-12-18 14:14:09

标签: python tkinter

所以有这个stackoverflow问题:

Switch between two frames in tkinter

这看起来像构建应用程序的美丽基础。但是如果在startPage上我希望有多个按钮,并在PageOne上显示相应的数据呢?这是我的尝试:

import tkinter as tk
from functools import partial

characters = {"character1": "abc", "character2": "def", "character3": "ghi", "character4": "jkl", "character5": "mnp", "character6": "qrs" }

class AdventureGame:
    ''' controller for managiging frames '''
    def __init__(self, master):
        self.master = master
        self.frames = {}
        for F in (StartGame,ShowCharacters,CharacterDetail):
            frame = F(parent=master, controller=self)
            self.frames[F.__name__] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        ''' rais the first frame '''
        self.raise_frame("StartGame")

    def raise_frame(self,page_name):
        ''' raise a frame '''
        frame = self.frames[page_name]
        frame.tkraise()

    def lower_frame(self,page_name):
        ''' lower a frame '''
        frame = self.frames[page_name]
        frame.lower()

class StartGame(tk.Frame):
  ''' introduction screen of the game '''
  def __init__(self, parent, controller):
    super(StartGame,self).__init__(parent)
    self.controller = controller
    ''' introduction text '''
    tk.Label(self, text="welcome to this game").grid()
    tk.Button(self, text="next", command=lambda: controller.raise_frame("ShowCharacters")).grid()

class ShowCharacters(tk.Frame):
  ''' main frame, overview of all pokemon '''
  def __init__(self, parent, controller):
    super(ShowCharacters,self).__init__(parent)
    self.controller = controller
    for row, character in enumerate(characters):
      name_label = tk.Label(self, text=characters[character])
      name_label.grid(row=row,column=0)
      info_button = tk.Button(self, text="view info", command=partial(self.switch,character))
      info_button.grid(row=row,column=1)

  def switch(self, character):
    test = self.controller.raise_frame("CharacterDetail")

class CharacterDetail(tk.Frame):
    ''' detail view of pokemon '''
    def __init__(self, parent, controller):
        super(CharacterDetail,self).__init__(parent)
        self.controller = controller
        tk.Button(self, text="back", command=lambda: controller.lower_frame("CharacterDetail")).grid()

     # somehow display characters name here
    def showdetail(self):
        tk.Label(self, text="test").grid()

def main():
    root = tk.Tk()
    app = AdventureGame(root)
    root.mainloop()
main()

感觉关键在控制器中,如下所述:

How to get variable data from a class

感觉就像我错过了什么,有人有想法吗?或者我在一起思考错误的方向?

如果我试图显示细节:

def switch(self, character):
    test = self.controller.raise_frame("CharacterDetail")
    CharacterDetail.showdetail(self)

它显示在ShowCharacters框架而不是CharacterDetail框架

我猜因为self是框架,它是来自ShowCharacters的self

我需要对控制器做些什么,但我的想法只是空白

所以现在我将信息传递给控制器​​:

import tkinter as tk
from functools import partial

characters = {"character1": "abc", "character2": "def", "character3": "ghi", "character4": "jkl", "character5": "mnp", "character6": "qrs" }

class AdventureGame:
    ''' controller for managiging frames '''
    def __init__(self, master):
        self.master = master
        self.frames = {}
        for F in (StartGame,ShowCharacters,CharacterDetail):
            frame = F(parent=master, controller=self)
            self.frames[F.__name__] = frame
            frame.grid(row=0, column=0, sticky="nsew")
        self.app_data = {"test": "test"}
        ''' rais the first frame '''
        self.raise_frame("StartGame")

    def raise_frame(self,page_name):
        ''' raise a frame '''
        frame = self.frames[page_name]
        frame.tkraise()

    def lower_frame(self,page_name):
        ''' lower a frame '''
        frame = self.frames[page_name]
        frame.lower()

class StartGame(tk.Frame):
  ''' introduction screen of the game '''
  def __init__(self, parent, controller):
    super(StartGame,self).__init__(parent)
    self.controller = controller
    ''' introduction text '''
    tk.Label(self, text="welcome to this game").grid()
    tk.Button(self, text="next", command=lambda: controller.raise_frame("ShowCharacters")).grid()

class ShowCharacters(tk.Frame):
  ''' main frame, overview of all pokemon '''
  def __init__(self, parent, controller):
    super(ShowCharacters,self).__init__(parent)
    self.controller = controller
    for row, character in enumerate(characters):
      name_label = tk.Label(self, text=characters[character])
      name_label.grid(row=row,column=0)
      info_button = tk.Button(self, text="view info", command=partial(self.switch,character))

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

  def switch(self, character):
    test = self.controller.raise_frame("CharacterDetail")
    CharacterDetail.example(self)

class CharacterDetail(tk.Frame):
    ''' detail view of pokemon '''
    def __init__(self, parent, controller):
        super(CharacterDetail,self).__init__(parent)
        self.controller = controller
        tk.Button(self, text="back", command=lambda: controller.lower_frame("CharacterDetail")).grid()
    def example(self):
        tk.Label(self, text=self.controller.app_data["test"]).grid()


def main():
    root = tk.Tk()
    app = AdventureGame(root)
    root.mainloop()
main()

标签仍然在错误的框架上,我如何才能访问正确的自我,以便我可以将标签添加到正确的框架中?

1 个答案:

答案 0 :(得分:1)

当您在课程CharacterDetail.example(self)的功能switch(...)中致电ShowCharacters时,self将成为课程ShowCharacters。因此,在example(self)类的函数CharacterDetail中创建的标签将位于ShowCharacters框架中。

尝试:

  • 返回类raise_frame(...)的函数AdventureGame中的凸起框架:

    def raise_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()
        return frame   # return the raised frame
    
  • 修改类switch(...)的函数ShowCharacters

    def switch(self, character):
        frame = self.controller.raise_frame("CharacterDetail")
        frame.example()