2016-08-19 63 views
1

我試圖用Tkinter的抓住從我的main.py文件的輸入,然後使用該輸入runAnalytics.py的Python AttributeError的:模塊「runAnalytics」有沒有屬性「跑」

main.py

import runAnalytics 
import tkinter 
import os 
import centerWindow 

loadApplication = tkinter.Tk() 
loadApplication.title("Stock Analytics") 
loadApplication.geometry("1080x720") 

label1 = tkinter.Label(loadApplication, text = "Ticker") 
input1 = tkinter.Entry(loadApplication) 

loadAnalytics = tkinter.Button(loadApplication, text = "Load Analytics", command = runAnalytics.run) 

centerWindow.center(loadApplication) 

loadAnalytics.pack() 
label1.pack() 
input1.pack() 

loadApplication.mainloop() 

runAnalytics.py

from yahoo_finance import Share 
from main import input1 
import tkinter 
import os 
import centerWindow 


def run(): 
    ticker = input1 
    loadAnalytics = tkinter.Tk() 
    loadAnalytics.title("$" + ticker + " Data") 
    loadAnalytics.geometry("1080x720") 

    print ("Price per share: " + ticker.get_price()) 

    ticker.refresh() 
    print ("Price per share: " + ticker.get_price()) 

    print("The dividend yield is: " + ticker.get_dividend_yield()) 

    print("The 52 week low is: " + ticker.get_year_low()) 
    print("The 52 week high is: " + ticker.get_year_high()) 
    print("The volume is: " + ticker.get_volume()) 

    print("The previous close was: " + ticker.get_prev_close()) 
    print("The previous open was: " + ticker.get_open()) 

    loadAnalytics.mainloop() 

我的錯誤消息的內容如下;

Traceback (most recent call last): 
    File "C:\Users\MyName\Documents\Python Projects\MarketData\main.py", line 1, in <module> 
    import runAnalytics 
    File "C:\Users\MyName\Documents\Python Projects\MarketData\runAnalytics.py", line 2, in <module> 
    from main import input1 
    File "C:\Users\MyName\Documents\Python Projects\MarketData\main.py", line 13, in <module> 
    loadAnalytics = tkinter.Button(loadApplication, text = "Load Analytics", command = runAnalytics.run) 
AttributeError: module 'runAnalytics' has no attribute 'run' 
+0

循環導入問題。重構你的代碼,使你沒有兩個模塊相互導入。 – user2357112

回答

3

你有一個圓形的進口:

import runAnalytics 
# .. 
from main import input1 

通過main被再次導入時,runAnalytics尚未有機會執行def run(): ..部分。

def run(input1): 

main.py模塊在通過此當調用函數::

loadAnalytics = tkinter.Button(loadApplication, text = "Load Analytics", command = lambda: runAnalytics.run(input1)) 

通過該對象在作爲參數而不是解決此除去from main import input1線,並且通

除了圓形的進口,也有問題,你運行任何文件在Python主腳本將被存儲爲__main__模塊。導入相同的腳本將再次導致模塊創建,現下設名main,並且該模塊中創建的任何對象都不同於__main__不同。

接下來,你要刪除runloadAnalytics.mainloop()通話,你不應該從一個已經運行的循環開始了新的主循環。您可能還想創建一個新的TopLevel窗口,而不是創建另一個Tk()根目錄。你必須在loadApplication傳遞給run太多,如果你走這條路。

+0

這比我的回答好得多,因爲它涵蓋了這裏的兩個問題。 –

+0

非常感謝,這解決了我的錯誤! – Andrew

-2

嘗試將()添加到runAnalytics.run的末尾。您正在告訴它尋找一個run屬性,它不具備的,而不是一個功能

loadAnalytics = tkinter.Button(loadApplication, text = "Load Analytics", command = runAnalytics.run()) 
+0

這將無法正常工作,因爲它會在按鈕聲明後立即運行。 –

+0

你可以這樣做:'loadAnalytics = tkinter.Button(loadApplication,文本= 「加載分析」,命令=拉姆達:runAnalytics.run())' –

+0

是的,你的答案更有意義 –

1
from runAnalytics import run 

loadAnalytics = tkinter.Button(loadApplication, text="Load Analytics", command=run) 

你不想開始tk的另一mainloop。相反,您應該通過root並創建一個頂層窗口。

def run(root): 
    ticker = input1 
    parent = Toplevel(root) 
    parent.title("$" + ticker + " Data") 
    # the rest of your code 
+0

由於循環導入,這仍然不起作用。在'runAnalytics'中定義'run'之前,'from main import input1'行會運行,並且'runAnalytics import run'將會失敗。 –

+0

@MartijnPieters是的,你是正確的,我覆蓋了其他問題,我看到你的答案現在涵蓋了兩個。謝謝! –

相關問題