2017-09-02 74 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

哇!只是这个?非常感谢! –

相关问题