Tkinter窗口未打开且窗口小部件未显示

时间:2020-04-07 05:36:10

标签: python tkinter

我创建了一个Tkinter程序,但是什么都没有显示!如果我在root = tk.Tk()的开头添加__init__(),则不会显示任何小部件!

import Tkinter as tk

class Interface(tk.Frame):
    def __init__(self, userKey, root):
        self.userKey = userKey
        tk.Frame.__init__(self, root)
        self.frame = tk.Frame(root)

        btn_users = tk.Button(self.frame, text='Users', command=self.users)
        btn_fullChain = tk.Button(self.frame, text='Chain', command=self.full_chain)
        btn_newTransaction = tk.Button(self.frame, text='New Transaction', command=self.Transaction)

        btn_users.pack()
        btn_fullChain.pack()
        btn_newTransaction.pack()

    def mine(self):
        mining = tk.Label(self.frame, text='Mining the current block, please wait')
        mining.pack()
        mine()
        completedMining = tk.Label(self.frame, text='Completed the mining process')
        mining.pack_forget()
        completedMining.pack()
        time.sleep(3)
        completedMining.pack_forget()

    def users(self):
        for widget in self.frame.winfo_children():
            widget.destroy()
        users = tk.Label(self.frame, text=blockchain.users)
        users.pack()

    def full_chain():
        for widget in self.frame.winfo_children():
            widget.destroy()
        entireChain = tk.Label(self.frame, text=blockchain.chain)
        entireChain.pack()

    def Transaction(self, sender, recipient, amount, signature):
        self.dataTransactions = {'sender': sender, 'recipient': recipient, 'signature': signature, 'amount': amount}
        new_transaction(self.dataTransactions)

    def new_transactions(self):
        for widget in self.frame.winfo_children():
            widget.destroy()

        backBTN = tk.Button(self.frame, text='Back', command=self.main)

        Recipient_Them = tk.Label(self.frame, text='Recipient')
        Recipient_Them.pack()

        Recipient = tk.Text()
        Recipient.pack()

        AmountData = tk.Label(self.frame, text='Amount')
        AmountData.pack()

        Amount = tk.Text()
        Amount.pack()

        SignatureData = tk.Label(self.frame, text='Signature')
        SignatureData.pack()

        Signature = tk.Text()
        Signature.pack()

        self.TransactionRecipient =  Recipient.get('1.0', END)
        self.TransactionAmount = Amount.get('1.0', END)
        self.TransactionSignature = Signature.get('1.0', END)
        self.TransactionSender = self.userKey

        btn_submit = tk.Button(self.frame, text="Submit", command=self.Transaction(self.TransactionSender, self.TransactionRecipient, self.TransactionAmount, self.TransactionSignature))
        btn_submit.grid(row=0, column=0, sticky="nsew")

    def main(self):
        for widget in self.frame.winfo_children():
            widget.destroy()

        btn_users = tk.Button(self.frame, text='Users', command=self.users)
        btn_fullChain = tk.Button(self.frame, text='Chain', command=self.full_chain)
        btn_newTransaction = tk.Button(self.frame, text='New Transaction', command=self.Transaction)

        btn_users.pack()
        btn_fullChain.pack()
        btn_newTransaction.pack()


if __name__ == '__main__':
    from argparse import ArgumentParser

    root = tk.Tk()
    parser = ArgumentParser()

    parser.add_argument('-key', '--nodeKey', default="0", type=str, help='key for this node')
    args = parser.parse_args()
    node_public_key = args.nodeKey

    if node_public_key == "0":
        raise ValueError("You must specify a node key!")

    UI = Interface(node_public_key, root)
    UI.pack()
    root.mainloop()

1 个答案:

答案 0 :(得分:0)

您将小部件放在self.frame内,而您从未在pack上调用placegridself.frame

目前尚不清楚为什么要创建该框架。 Interface本身就是一个框架,因为您是从tk.Frame继承的。您应该删除self.frame,并在各处而不是self使用self.frame。如果这样做,您将看到您的按钮。