2017-09-02 25 views
0

我建立一個小項目,其中涉及有他們單獨的功能4個Python文件。但是,有一個main.py文件通過導入它們來使用所有其他文件。連接相同的GUI到兩個或更多的Python文件

現在,我已經建立了這個項目,我是main.py文件中建立一個GUI。我的問題是,一些其他的文件具有print控制檯,當整個項目的運作上,我要上的GUI print這些功能,而不是功能。那麼,如何打印在主文件中創建一個Text字段從其他文件中的文本。

main.py

import second as s 
from tkinter import * 

def a(): 
    field.insert(END, "Hello this is main!") 

root = Tk() 
field = Text(root, width=70, height=5, bd=5, relief=FLAT) 
button1 = Button(root, width=20, text='Button-1', command=a) 
button2 = Button(root, width=20, text='Button-2', command=s.go) 
root.mainloop() 

second.py

def go(): 
    print("I want this to be printed on the GUI!") 
#... and a bunch of other functions... 

我只是想,當用戶按下按鈕-2,則函數go()打印文本上field

回答

1

我認爲它嘗試添加012的最佳方式作爲函數的參數go

這裏是我的代碼:

main.py

import second as s 
from tkinter import * 

def a(field): 
    field.insert(END, "Hello this is main!\n") 

root = Tk() 

field = Text(root, width=70, height=5, bd=5, relief=FLAT) 
button1 = Button(root, width=20, text='Button-1', command=lambda : a(field)) 
button2 = Button(root, width=20, text='Button-2', command=lambda : s.go(field)) 

#to display widgets with pack 
field.pack() 
button1.pack() 
button2.pack() 

root.mainloop() 

second.py

from tkinter import * 


def go(field): 
    field.insert(END, "I want this to be printed on the GUI!\n") 
    print("I want this to be printed on the GUI!") 
    #something you want 

的運行:)

效果截圖

+0

哇!只是這個?非常感謝! –

相關問題