我正在使用Tkinter開發下載管理器的Python GUI應用,在開發到一半時,我的代碼開始變得非常凌亂,所以我決定在不同的檔案中分離函式,然后再匯入它。 我的主代碼:
from tkinter import *
from functions import add_download
root = Tk()
root.title("The Pownloader!")
canvas = Canvas(root, width=700, height=500).pack()
# Button:
ADD_BUTTON = Button(root, text="ADD"/span>, bd=4, height=2, width=5, command=add_download)
SETTINGS_BUTTON = Button(root, text="SETTINGS"/span>, bd=4, height=2, width=5)
ABOUT_BUTTON = Button(root, text="關于", bd=4, height=2, width=5)
EXIT_BUTTON = Button(root, text="EXIT"/span>, bd=4, height=2, width=5, command=quit)
# 迷你按鈕:
PAUSE_MINI_BUTTON = Button(root, text="PAUSE", font=(None, "8"), height=2, width=3)
RESUME_MINI_BUTTON = Button(root, text="RESUME", font=(None, "8"/span>), height=2, width=3)
REMOVE_MINI_BUTTON = Button(root, text="REMOVE", font=(None, "8"), height=2, width=3)
# Side_Mini_Buttonons:
DOWNLOAD_WINDOW = Button(root, text="Downloads", font=(None, "8"), height=3, width=6)
ERRORS_WINDOW = Button(root, text="Failed", font=(None, "8"), height=3, width=6)
COMPLETED_WINDOW = Button(root, text="Completed", font=(None, "8"), height=3, width=6)
# 定位按鈕:
ADD_BUTTON.place(x=70, y=20)
SETTINGS_BUTTON.place(x=145, y=20)
ABOUT_BUTTON.place(x=220, y=20)
EXIT_BUTTON.place(x=295, y=20)
PAUSE_MINI_BUTTON.place(x=290, y=455)
RESUME_MINI_BUTTON.place(x=340, y=455)
REMOVE_MINI_BUTTON.place(x=390, y=455)
DOWNLOAD_WINDOW.place(x=1, y=100)
ERRORS_WINDOW.place(x=1, y=160)
COMPLETED_WINDOW.place(x=1, y=220)
# 下載框架:
DOWNLOAD_LIST_LABEL = Label(root, text="Download List:")
DOWNLOAD_LIST_LABEL.place(x=70, y=80)
DOWNLOAD_ENTRIES = Listbox(root, width=70, height=19)
DOWNLOAD_ENTRIES.place(x=70, y=100)
# 主回圈:
root.mainloop()
然而我的 functions.py 代碼看起來是這樣的:
def add_download() 。
# 定義彈出式框架:
top = Toplevel(root, width = 420, height = 150)
top.title("New Download")
# Putting on widgets:
link = StringVar()
LINK_LABEL = Label(top, text = "Paste Link:")
FIELD_ENTRY = Entry(top, width = 40, textvariable=link)
def on_click()。
link_to_verify = (link.get() ).strip()
if len(link_to_verify)>15:
if link_to_verify[0:11]=="http://www。" :
DOWNLOAD_ENTRIES.insert(0, link_to_verify)
else:
print("Stupid"/span>)
else:
print("not a valid link")
BUTTONS_WIDGET = Frame(top)
ADD_BUTTON = Button(BUTTONS_WIDGET, text = "Add" , width=10, command=on_click)
CANCEL_BUTTON = Button(BUTTONS_WIDGET, text = "取消", width=10, command=top.destroy)
# Positionning everythig:
LINK_LABEL.grid(column=0, row=0)
FIELD_ENTRY.grid(column=1, row=0)
BUTTONS_WIDGET.grid( column=1,row=2)
ADD_BUTTON.grid( column=0,row=0)
CANCEL_BUTTON.grid( column=1,row=0)
基本上,我想讓這個函式呼叫并顯示一個彈出視窗,我相信這可以做得更好,但我只是在學習,然而我收到一個錯誤說。 Toplevel is not defined
uj5u.com熱心網友回復:
你將需要建立一個類來管理它。
在run.py里面:import tkinter as tk
from interface import GUI
root = tk.Tk()
GUI(root)
然后在你的interface.py腳本中,你可以呼叫額外的模塊:
import tkinter as tk
from aux import AuxGUI
from menu import MenuGUI
class GUI。
def __init__(self, master):
self.master = master
self.GUI_list = []
self.AuxGUI = AuxGUI(self.master, self.GUI_list) # 附加模塊[/span
self.MenuGUI = MenuGUI (self.master, self.GUI_list) # 附加模塊。
然后你可以使用OOP來訪問函式或物件來動態地相互作用。
self.GUI_list.append(self.AuxGUI)
self.GUI_list.append(self.MenuGUI)
在menu.py中從GUI_list中識別出正確的索引:
import tkinter as tk
class MenuGUI。
def __init__(self, master, GUI_list)。
self.master = master
self.AuxGUI = GUI_list[0]
uj5u.com熱心網友回復:
每個檔案都需要匯入tkinter.
。此外,主檔案中任何被匯入的函式所需要的變數都需要被傳遞到函式中。例如,你應該定義add_download來接受根視窗作為引數。
def add_download(root)。
...
然后,在主程式中,將root作為該引數傳入:
ADD_BUTTON = Button(root, ..., command=lambda: add_download(root)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/326657.html
標籤:
