为什么我的按钮不打印设计功能?

时间:2019-05-15 08:21:27

标签: python button tkinter tabs command

这是我较大程序的较小版本。当我在“第1周”上点击“前进” Button时,它应该打印“嗨,从第2周开始” ,但是它仍在打印“嗨,从第1周开始。 “

只是确保我分配了正确的按钮小部件,因为该小部件出现在多个位置,因此我使用下面的行进行测试,并且它可用于所需的特定小部件。

self.parent.parent.sheet_dict["Week2"].upper_tabs_dict["Final"].button.configure(text = "red")

我不知道为什么我的forward(self):函数无法正常工作。

感谢您抽出宝贵的时间来帮助我。

这是完整的代码:

import tkinter as tk
from tkinter import ttk
from functools import partial

class Application(tk.Tk):
    def  __init__(self):
        super().__init__()
        self.title("notbook my own try")
        self.geometry("700x450")
        self.config(bg="tan")

        self.style = ttk.Style() # instantce of ttk.Style() class.
        self.lnb = Sheets(self) #composition (application has child)

class Sheets(ttk.Notebook): #contains the weeks tabs
    def __init__(self, parent): #parent is root, which is tk.
        parent.style.configure("down.TNotebook", tabposition="sw")
        super().__init__(parent, style = "down.TNotebook")# this parent   
               is Notebook class.
        self.pack(fill = tk.BOTH, expand =1)
        self.parent = parent

        self.week_index = ['Week 1', 'Week 2',"Week 3", "Week 4"] #
        self.sheet_dict = {} #holds all the One_Weekly_Notebook's 
                     instances

        for week in (self.week_index):
            self.week = One_Weekly_Notebook(self)#create instances
            self.week.pack()
            self.add(self.week, text = week)
            self.pack()
            self.sheet_dict[week] = self.week


class One_Weekly_Notebook(ttk.Notebook): #contains the tabs
    def __init__(self, parent):
        super().__init__(parent)
        self.pack(fill = tk.BOTH, expand =1)
        self.parent = parent

        self.upper_tabs_dict = {}
        self.calculate_buttons_dict = {} #to store all the calculate 
                  buttons in differnt weeks

        self.object_final = Final(self)
        self.object_requests = Requests(self)
        self.object_control = Control(self)

        tab1 = self.object_final #child of lower notebook
        tab2 = self.object_requests
        tab3 = self.object_control
        tab1.pack()
        tab2.pack()
        tab3.pack()

        self.add(tab1, text = "Final")
        self.pack()
        self.add(tab2, text = 'Requests')
        self.pack()
        self.add(tab3, text = 'Control')
        self.pack()

        self.upper_tabs_dict["Final"] = tab1
        self.upper_tabs_dict["Requests"] = tab2
        self.upper_tabs_dict["Control"] = tab3

    def display_results(self, button): #layer 3
        self.say_hi(button)

    def say_hi(self,button): #layer 3
         self.id =    
        self.parent.parent.lnb.index(self.parent.parent.lnb.select())+1
#             button=     
        self.parent.parent.lnb.index(self.parent.parent.lnb.select())+1
        print (f"Hi from {button}")#current tap #)

class Final(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.pack(fill=tk.BOTH, expand=1)
        self.parent = parent


        self.buttons()
    def buttons(self):
        self.button = tk.Button(self, text="Say Hi", bg="salmon")
        self.button.configure(command= lambda button=self.button:    
           self.parent.display_results(button))
        self.button.pack()
        self.parent.calculate_buttons_dict = self.button

        self.buttonc = tk.Button(self, text = "Forward", command =   
           self.forward)
        self.buttonc.pack()


        print (self.parent.calculate_buttons_dict)

    def forward(self):
        self.parent.display_results(self.parent.parent.sheet_dict["Week   
            1"].upper_tabs_dict["Final"].button)
        self.parent.display_results(self.parent.parent.sheet_dict["Week 
            2"].upper_tabs_dict["Final"].button)
        self.parent.display_results(self.parent.parent.sheet_dict["Week 
            3"].upper_tabs_dict["Final"].button)
        self.parent.display_results(self.parent.parent.sheet_dict["Week 
           4"].upper_tabs_dict["Final"].button)

class Requests(Final):
    pass

class Control(tk.Frame):
    pass

if __name__ == '__main__':
    Application().mainloop()


enter code here

1 个答案:

答案 0 :(得分:0)

我仍然无法完全理解您要达到的目标,但让我们细分为较小的部分。

首先,在您当前的forward函数中:

self.parent.parent.sheet_dict["Week 2"].upper_tabs_dict["Final"].button.configure(command= self.parent.display_results)

这仅修改button命令。从外观上,您想改为执行命令。如果是这样,只需直接调用该方法:

self.parent.parent.sheet_dict["Week 2"].display_results()

接下来,您还说过,当您单击“第1周”上的“前进”按钮时,希望打印“第2周嗨”,但它仍在打印“第1周嗨”。实际上,您现在就已经编码了它,并按照它的方式进行了编码。通过您的say_hi方法:

print (f"Hi from {self.parent.parent.lnb.index(self.parent.parent.lnb.select())+1}")

您正在查找当前笔记本的索引。因此,即使您指定了sheet_dict["Week 2"],它也将始终打印当前的标签索引。

我的建议是在创建笔记本时首先将索引包括在内:

class Sheets(ttk.Notebook):
    def __init__(self, parent):
        ...
        for num, week in enumerate(self.week_index,1): #pass an index, starting from 1
            self.week = One_Weekly_Notebook(self,num)
            self.week.pack()
            self.add(self.week, text = week)
            self.pack()
            self.sheet_dict[week] = self.week

class One_Weekly_Notebook(ttk.Notebook):
    def __init__(self, parent,index):
        super().__init__(parent)
        self.pack(fill = tk.BOTH, expand =1)
        self.index = index #store the index as an attribute
        self.parent = parent
        ...

    def say_hi(self): #layer 3
        print (f"Hi from {self.index}")

然后,它应始终返回forward方法中指定的正确索引。