2017-03-05 125 views
0

我有兩個文件,一個包含tkinter代碼,另一個包含一個函數。我在tkinter窗口中有一個按鈕和一個Entry字段。我試圖在單擊按鈕時執行該功能,但它需要Entry字段中的文本才能工作。試圖從Tkinter的文件導入任何東西,當我得到一個錯誤:Tkinter - 在另一個文件中使用Entry值,它本身導入到tkinter文件中

tkinter_file.py:

import File 
window = Tk() 
def input(): 
    s = entry1.get() 
    return s 

entry1 = Entry(window) 
button1 = Button(window, text='GO', command=File.function) 

File.py:

from tkinter import * 
import tkinter_file 

def function(): 
    req_url = 'http://someurl.com/{}'.format(tkinter_file.input) 
    requests get url etc. etc. 

我似乎儘快得到一個錯誤我導入tkinter_fileFile.py,甚至只是功能input

File "/etc/etc/tkinter_file.py", line 75, in <module> 
button1 = Button(window, text='GO', command=File.function) 
AttributeError: module 'File' has no attribute 'function' 

我在想req_url沒有值s直接是問題,以及可能導入對方的2個文件,但你如何克服這一點?

謝謝

回答

2

如果你有兩個模塊,說a.pyb.py,你不能在a導入模塊b,然後在b導入模塊a,因爲這將創建一個的依賴,它可以」明確地解決!

一個解決方案將作爲參數傳遞給File.function您需要該功能才能正常運行,即entry1的內容。

button1 = Button(window, text='GO', command=lambda: File.function(entry1.get())) 
+0

這麼簡單,我什至沒有想到它。謝謝! – StevenH