2017-05-09 97 views
0

我正在尋找添加信息到下面的這些按鈕,所以當按下信息出現....我一直在嘗試給我們的get()功能,這是正確的嗎?我似乎無法得到它的運行,但我的繼承人的代碼嘗試:添加信息到按鈕

submit = Button(frame, text="Enter", width=15, command=lambda: valueGET(E1.get(), E2.get())) 
submit.grid() 

和我的全碼:

def raise_frame(frame): 
     frame.tkraise() 

    f1 = tk.Frame(root) 
    f2 = tk.Frame(root) 
    f3 = tk.Frame(root) 
    f4 = tk.Frame(root) 
    f5 = tk.Frame(root) 

    for frame in (f1,f2,f3,f4,f5): 
     frame.grid(row=0, column=0, sticky='news') 

class MyButton(tk.Button): 
    def __init__(self, *args, info=None, command=None, **kwargs): 
     super().__init__(*args, **kwargs, command=self.callback) 

     self.initialCommand = command 
     self.info = info 

    def display_info(self): 
     # Display the information the way you want 
     print(self.info) 

    def callback(self): 
     self.initialCommand() 
     self.display_info() 



    button1 = tk.Button(f1, text='Ya', command=lambda:raise_frame(f2)).pack() 
    button2 = tk.Button(f1, text=Yb', command=lambda:raise_frame(f3)).pack() 
    button3 = tk.Button(f1, text=Yc', command=lambda:raise_frame(f4)).pack() 
    button4 = tk.Button(f1, text='Yd', command=lambda:raise_frame(f5)).pack() 




    tk.Label(f2, text="Ya").pack() 
button = tk.Button(root, text="Display info", command=lambda:print("Initial command")) 
    button.pack() 
    button.info = "Hello, world!" 
     tk.Button(f2, text="HOME", command=lambda:raise_frame(f1)).pack() 

     tk.Label(f3, text="Yb").pack() 
button = tk.Button(root, text="Display info", command=lambda:print("Initial command")) 
    button.pack() 
    button.info = "Hello, world!" 
     tk.Button(f3, text="HOME", command=lambda:raise_frame(f1)).pack() 

     tk.Label(f4, text="Yc").pack() 
button = tk.Button(root, text="Display info", command=lambda:print("Initial command")) 
    button.pack() 
    button.info = "Hello, world!" 
     tk.Button(f4, text="HOME", command=lambda:raise_frame(f1)).pack() 

     tk.Label(f5, text="Yd").pack() 
button = tk.Button(root, text="Display info", command=lambda:print("Initial command")) 
    button.pack() 
    button.info = "Hello, world!" 
     tk.Button(f5, text="HOME", command=lambda:raise_frame(f1)).pack() 

    raise_frame(f1) 
    root.mainloop() 

if os.path.isfile(creds): 
    Login() 
else: 
    Signup() 
+0

第一:如果你有兩個問題,你應該問兩個問題。 秒:你的'sequentialSearch'函數查找單個項目。字符串中的單詞是子序列。你的功能只能找到單個字符。所以我建議你將'if'語句改爲'if alist [pos:pos + len(list(item))] == list(item):' – Aemyl

+0

「你到底意味着什麼」將信息添加到按鈕「? –

+0

添加文本以便按下按鈕後,信息出現在選項卡 – joe

回答

1

據我瞭解,你要存儲的特定信息,與一個特定的按鈕,以便當按下所述按鈕時,信息顯示在某處。

您可以擴展Button類,以便使包裝能夠保存該信息。

import tkinter as tk 

class MyButton(tk.Button): 
    def __init__(self, *args, info=None, command=None, **kwargs): 
     super().__init__(*args, **kwargs, command=self.callback) 

     self.initialCommand = command 
     self.info = info 

    def display_info(self): 
     # Display the information the way you want 
     print(self.info) 

    def callback(self): 
     self.initialCommand() 
     self.display_info() 

這允許您通過設置「nfo`屬性來隨時設置包裝信息。

將此類添加到您的代碼的開頭,並將所有Button實例替換爲MyButton實例。

新按鈕的行爲應與以前完全相同,只是按下它們時,它們的info屬性將按照您在實例上傳遞的命令的調用後,按照您在display_info方法中定義的方式顯示。

您需要根據您的需要定義display_info(在控制檯中打印,顯示在標籤中...)。


下面是一個簡單的例子:

root = tk.Tk() 
button = MyButton(root, text="Display info", command=lambda:print("Initial command")) 
button.pack() 
button.info = "Hello, world!" 

root.mainloop() 

上面的代碼顯示一個按鈕根窗口。當按下此按鈕時,控制檯中將打印Initial command,然後打印info屬性,即"Hello, world!"

+0

並且使用我當前的代碼,我會在何處放置它以使其工作?即時通訊面臨重複性錯誤。此外,我希望這樣做,所以當你按下按鈕時,信息出現在打開的窗口上,而不是新的窗口 – joe

+0

@joe我編輯了我的帖子和代碼。您需要定義'display_info'方法,以便以您想要的方式實際顯示信息。 –

+0

我會編輯我的代碼,並告訴你我有什麼,它仍然無法正常工作。我也在這行上得到了一個錯誤消息super().__ init __(* args,** kwargs,command = self.callback)from逗號 – joe