0

には、Appjar packageを使用してPythonでスレッドを処理しようとする問題がありません。python appjar関数はスレッド

次のプログラムは、リストをカウントし、進行状況バーを同時に更新する必要があります。私はappjar documentation for threadingに従ってきましたが、それはあなたが関数のparamsを挿入することを意図しているここでapp.thread(ライン35)、中NameError: name 'percent_complete' is not definedを返すだ - 私のコードは以下の通りです:私はしてエラーを取り除くことができます

from appJar import gui 
import time 

# define method the counts through a list of numbers, and updates the progress meter 

def press(btn): 
    objects = [1,3,6] 
    total = len(objects) 
    current_object = 0 
    for i in objects: 
     print(i) 
     current_object += 1 
     current_percent_complete = (current_object/total) * 100 
     updateMeter(current_percent_complete) 
     time.sleep(1) 

def updateMeter(percent_complete): 
    app.queueFunction(app.setMeter, "progress", percent_complete) 

# create a GUI variable called app 

app = gui("Login Window") 
app.setBg("orange") 
app.setFont(18) 

# add GUI elements : a label, a meter, & a button 

app.addLabel("title", "COUNTER") 
app.setLabelBg("title", "blue") 
app.setLabelFg("title", "orange") 

app.addMeter("progress") 
app.setMeterFill("progress", "green") 

app.addButton("START COUNTING", press) 

# put the updateMeter function in its own thread 

app.thread(updateMeter, percent_complete) 

# start the GUI 

app.go() 

しかし

from appJar import gui 
import time 

# define method the counts through a list of numbers, and updates the progress meter 

percent_complete = 0 

def press(btn): 
... 

、GUI負荷とボタンが押されたとき、それはスレッドはありません。そうのようなpercent_completeを定義します。その代わりに、リストを反復して、その後、進行状況バーを更新します。

誰もが同じ問題を抱えていますか?どんな洞察もすごく感謝しています! ありがとう!

答えて

0

問題のカップルがここにあります

  • まず、私はあなたの数学はとメーターを更新するために、良い割合になるか分からないので、あなたは多くの変更が表示されないことがあります - あなたが使用する必要がありますi

  • 第2に、ループ(およびその内部のスリープ)がすべて完了するまで、GUIは更新されません。代わりに、あなたがプロセスにどのように多くのアイテムを数えてみてください、とafter()機能でそれらを反復処理する必要があり、ここを参照してください:http://appjar.info/pythonLoopsAndSleeps/#conditional-loops

  • 第三に、最後にapp.thread()への呼び出しがはるかに達成しない - それはupdate_meter()関数を呼び出しますパラメータが存在しない場合は削除することができます。あなた一度、この試してみても除去することができます...

」 - あなたは本当にスレッドを使用していないよう

  • 第四には、実際のupdate_meter()機能は、必要ありません

    current_object = 0 
    def press(btn): 
        global current_object 
        current_object = 0 
        processList() 
    
    def processList(): 
        global current_object 
        objects = [1,3,6] 
        total = len(objects) 
        if current_object < total: 
         i = objects[current_object] 
         print(i) 
         current_object += 1 
         current_percent_complete = (current_object/total) * 100 
         app.setMeter("progress", current_percent_complete) 
         app.after(1000, processList) 
    

    UPDATE:はちょうど数学の問題について明確にするために、あなたは別のことで1つの整数分周している:0/3を、数学を見ていたVEの1/3、2/3、3/3などとなる。 python2では、これは0になり、python3では分数が得られます。