[英]Having TKinter GUI in seperate .py file and binding functions to buttons in the GUI file
在用 C# 編程幾年之后,我正在嘗試自學用 Python 編寫代碼。 到目前為止,我發現過渡非常困難,但希望通過一些堅持,我最終會理解 python
我的問題:在嘗試過像 wxglade 和 wxformbuilder 這樣的程序后,我被這些程序為您提供一個漂亮的單獨 GUI 文件的方式所吸引,這樣您就可以將所有功能放在一個單獨的主文件中。 但是,我想將相同的原則應用於 tkinter GUI,但我無法弄清楚,也找不到有關它的文檔。
假設我有兩個文件:GUI.py:
from tkinter import *
class BotGUI:
def __init__(self, master):
# ***** Creation of the frame for all items to be on *****
xWidth=800
yWidth=500
margin=100
outer_frame=Frame(master, width=xWidth, height=yWidth, bg='lightgray')
outer_frame.pack(ipadx=margin, ipady=margin)
button1 = Button(outer_frame, text = "Print something", command = printcommand)
button1.pack()
#Virtual event handlers
def printcommand(self, event):
event.Skip()
然后是一個名為 Main.py 的文件:
from tkinter import *
import GUI
class GUI_Functions(GUI.BotGUI):
def __init__(self,parent):
GUI.BotGUI.__init__(self, parent)
def printcommand(self, event):
print("The bind was successful!")
if __name__ == '__main__':
root = Tk()
GUI_Frame = GUI.BotGUI(root)
root.mainloop()
上面的代碼導致錯誤:
NameError: global name 'printcommand' is not defined
我只是不知道如何讓它在 tkinter GUI 上工作。 希望有人能幫我一下!
(使用 Visual Studio 在 C# 中創建 GUI 容易得多:()
您收到一個錯誤,因為 Python 不知道在哪里可以找到函數printcommand
。
首先導入文件Main.py
以便您可以在GUI.py
使用它。
import Main.py
現在的問題是方法printcommand
是一個實例方法,這意味着您需要有一個對象來調用它。
為了實現這一點。
GUIFunctions
類的實例並使用該對象引用該方法guifuncs = Main.GUIFunctions(master)
command = guifuncs.printcommand
注意:您必須前綴以Main.py引用Main
與上面所用的進口語法。
我希望這對您有所幫助,如果您有任何其他問題,請隨時在下面發表評論!
Python 找不到一個名為“printcommand”的全局函數,確實沒有全局函數“printcommand”。 您必須通過添加self.
來告訴button1
回調命令是一個本地函數self.
到回調定義。 它應該是這樣的:
command = self.printcommand
這將導致另一個問題,即按鈕回調不會將事件傳回。 我通過刪除事件參數解決了這個問題,這樣 GUI.py 中的 printcommand 函數看起來像這樣:
def printcommand(self):
pass
如果您確實需要將值傳遞給回調函數,那么 Effbot上的這個頁面是一個不錯的起點。
正如 Micheal 所回答的,我的 GUI.py 文件中缺少“import Main”,在運行該文件后,我在代碼中發現了其他一些小錯誤,因此請在我用來運行它的代碼下方找到:
GUI.py 文件:
from tkinter import *
import Main
class BotGUI:
def __init__(self, master):
# ***** Import the guifunctions from the Main.py file
guifuncs = Main.GUI_Functions(master)
# ***** Creation of the frame for all items to be on *****
xWidth=800
yWidth=500
margin=100
outer_frame=Frame(master, width=xWidth, height=yWidth, bg='lightgray')
outer_frame.pack(ipadx=margin, ipady=margin)
button1 = Button(outer_frame, text = "Print something", command = guifuncs.printcommand)
button1.pack()
和 Main.py 文件:
from tkinter import *
import GUI
class GUI_Functions():
def __init__(self,parent):
pass
def printcommand(self):
print("The bind was successful!")
if __name__ == '__main__':
root = Tk()
GUI_Frame = GUI.BotGUI(root)
root.mainloop()
感謝 Micheal 指出問題! 還要感謝 Alex 提供了指向 effbot.org 的有用鏈接!
希望這會有所幫助。
創建一個名為“guiFactory.py”的文件
def createButton(self, Button, text, command, highlightbackground, compound, x, y):
self.nav_button = Button(self, text =text, command=command, highlightbackground=highlightbackground, compound=compound)
self.nav_button.place(x = x, y = y)
在您的主應用程序文件中。
import tkinter as tk
from tkinter import StringVar, IntVar, messagebox, OptionMenu, Button, Label, filedialog as fd
import guiFactory
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
# Canvas Size
parent.geometry("300x350")
self.configure(bg="#3E4149")
parent.title("Test Application")
# Submit Button
def convertFilesHandler():
print("Do Something")
# buttons
convertBtn = guiFactory.createButton(self, Button, "Convert", convertFilesHandler, "#3E4149", "center", 200, 250)
if __name__ == "__main__":
root = tk.Tk()
MainApplication(root).pack(side="top", fill="both", expand=True)
root.mainloop()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.