繁体   English   中英

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

[英]Why is my button not printing designed function?

这是我较大程序的较小版本。 当我在“第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

我仍然无法完全理解你想要实现的目标,但让我们分解成更小的部分。

首先,在您当前的forward功能中:

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

这只会修改按钮命令。 从它的外观来看,你想要执行命令。 如果是这样,只需直接调用该方法:

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方法中指定的正确索引。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM