在Tkinter中导入具有功能的外部文件时出现问题

时间:2020-07-25 06:12:35

标签: python tkinter

我正在使用tkinter在程序的GUI上工作。我做了一个单击按钮时会打开一个窗口的函数。我将该函数放在一个外部文件中,以避免混乱和混乱。

主要代码是:

from tkinter import *
from Configure_Encryption_Keys_Window import *

# Main root menu
root = Tk()
root.title("Master of Enc & Dec")
root.geometry("830x570")

# Creating the "Configure Encryption Keys" Button.
Configure_Encryption_Keys = Button(root, text="Configure Encryption Keys", borderwidth=2, command=Toplevel)
Configure_Encryption_Keys.pack()
Configure_Encryption_Keys.place(x=100, y=0)

# Creating the "Quit" Button.
Quit = Button(root, text="Quit", fg="red", command=root.quit, borderwidth=2)
Quit.pack()
Quit.place(x=795, y=0)

# Creating the "Configure Decryption Keys" Button.
Configure_Decryption_Keys = Button(root, text="Configure Decryption Keys", borderwidth=2)
Configure_Decryption_Keys.pack()
Configure_Decryption_Keys.place(x=500, y=0)

# Label for the Encryption input Entry.
Label_For_Input_Text_To_Be_Encrypted = Label(root, text="Enter Text To Be Encrypted:")
Label_For_Input_Text_To_Be_Encrypted.pack()
Label_For_Input_Text_To_Be_Encrypted.place(x=102, y=40)

# Creating the Encryption input entry.
Input_Text_To_Be_Encrypted = Text(root, wrap=WORD)
Input_Text_To_Be_Encrypted.pack()
Input_Text_To_Be_Encrypted.place(x=15, y=60, height=200, width=375)

# Scrollbar for the Encryption input entry.
Scroll1 = Scrollbar(root, command=Input_Text_To_Be_Encrypted.yview)
Scroll1.pack()
Scroll1.place(x=390, y=60, height=200)
Input_Text_To_Be_Encrypted.config(yscrollcommand=Scroll1.set)

# Button for Encryption process.
Encrypt = Button(root, text="Encrypt", borderwidth=2)
Encrypt.pack()
Encrypt.place(x=160, y=265)

# Label fot the Decryption input Entry.
Label_For_Input_Text_To_Be_Decrypted = Label(root, text="Enter Text To Be Decrypted:")
Label_For_Input_Text_To_Be_Decrypted.pack()
Label_For_Input_Text_To_Be_Decrypted.place(x=502, y=40)

# Creating the Decryption input Entry.
Input_Text_To_Be_Decrypted = Text(root, wrap=WORD)
Input_Text_To_Be_Decrypted.pack()
Input_Text_To_Be_Decrypted.place(x=430, y=60, height=200, width=375)

# Scrollbar for the Decryption input entry.
Scroll2 = Scrollbar(root, command=Input_Text_To_Be_Decrypted.yview)
Scroll2.pack()
Scroll2.place(x=805, y=60, height=200)
Input_Text_To_Be_Decrypted.config(yscrollcommand=Scroll2.set)

# Button for Decryption process.
Decrypt = Button(root, text="Decrypt", borderwidth=2)
Decrypt.pack()
Decrypt.place(x=560, y=265)

# Label for the Encrypted Result output Entry.
Label_For_Encrypted_Text = Label(root, text="Encrypted Text:")
Label_For_Encrypted_Text.pack()
Label_For_Encrypted_Text.place(x=140, y=300)

# Creating the Encrypted Result output Entry.
Encrypted_Output = Text(root, wrap=WORD)
Encrypted_Output.pack()
Encrypted_Output.place(x=15, y=320, height=200, width=375)

# Scrollbar for the Encrypted output Entry.
Scroll3 = Scrollbar(root, command=Encrypted_Output.yview)
Scroll3.pack()
Scroll3.place(x=390, y=320, height=200)
Encrypted_Output.config(yscrollcommand=Scroll3.set)

# Creating Copy Button for the Encrypted Text.
Copy_Encryption = Button(root, text="Copy Encryption", borderwidth=2)
Copy_Encryption.pack()
Copy_Encryption.place(x=135, y=530)

# Label for the Decrypted Result output Entry.
Label_For_The_Decrypted_Text = Label(root, text="Decrypted Text:")
Label_For_The_Decrypted_Text.pack()
Label_For_The_Decrypted_Text.place(x=535, y=300)


# Creating the Decrypted Result Output Entry.
Decrypted_Output = Text(root, wrap=WORD)
Decrypted_Output.pack()
Decrypted_Output.place(x=430, y=320, height=200, width=375)

# Scrollbar for the Decrypted Result output Entry.
Scroll4 = Scrollbar(root, command=Decrypted_Output.yview)
Scroll4.pack()
Scroll4.place(x=805, y=320, height=200)
Decrypted_Output.config(yscrollcommand=Scroll4.set)

# Creating Copy Button for the Decrypted Text.
Copy_Decryption = Button(root, text="Copy Decryption", borderwidth=2)
Copy_Decryption.pack()
Copy_Decryption.place(x=540, y=530)

root.mainloop()
Toplevel.mainloop()

外部代码为:

from Encrypter_and_Decrypter_software import *
from tkinter import *
 # Configure Encryption Keys Window
def Configure_Encryption_Keys_Window():
    Configure_Encryption_Keys_Window = tk.Toplevel()
    Toplevel = Tk(root)
    Toplevel.title("Master o")
    Toplevel.geometry("830x570")

当我第一次运行该程序并单击“配置加密密钥”按钮时,将打开一个新窗口。但这不是我想要的大小或标题。

退出程序时出现的错误是:

Traceback (most recent call last):
  File "C:\Users\Noori\OneDrive\Desktop\Encrypter and Decryption code\Encrypter_and_Decrypter_software.py", line 2, in <module>
    from Configure_Encryption_Keys_Window import *
  File "C:\Users\Noori\OneDrive\Desktop\Encrypter and Decryption code\Configure_Encryption_Keys_Window.py", line 1, in <module>
    from Encrypter_and_Decrypter_software import *
  File "C:\Users\Noori\OneDrive\Desktop\Encrypter and Decryption code\Encrypter_and_Decrypter_software.py", line 110, in <module>
    Toplevel.mainloop()
TypeError: mainloop() missing 1 required positional argument: 'self'

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

Toplevel是python中的预定义变量。如果您将其用作局部变量,请进行更改。

或将其用作

a=Toplevel(root)
a.mainloop()

顶层用于在tki tera中打开辅助窗口