簡體   English   中英

進度欄不會關閉python 3.4 tkinter

[英]Progressbar is not going down python 3.4 tkinter

我正在嘗試清空已滿的進度條,模擬注射器的行為,但由於某些原因無法正常工作。

用一個按鈕我叫mAspire ,先叫sEmpty(),然后是sUp(),然后是sStop()然后是sFull(),因此它將創建2個空白的進度條,然后將其填充為函數“ .step”,然后停止這些進度欄,然后創建2個完整的進度欄

用一個按鈕,我調用mDispense ,它依次調用sFull(),sDown(),sStop()和sEmpty(),因此它將創建2個Full progressbar,然后使用函數“ .step”清空它們,然后停止這些進度欄,然后創建2個空白進度欄

為什么不工作?

救命

import time
import serial
import sys
import os
import tkinter as tk
from tkinter import ttk
from tkinter import *
try:
    import Tkinter
    import ttk
except ImportError:
    import tkinter as Tkinter
    import tkinter.ttk as ttk

mGui = Tk()
mGui.title("try bar")
mGui.geometry('1250x650+10+10')

BarVolSyringe1 = ttk.Progressbar(mGui, orient='vertical', length=78,
                                 mode='determinate')
BarVolSyringe2 = ttk.Progressbar(mGui, orient='vertical', length=78,
                                 mode='determinate')
BarVolSyringe1.place(x=952,y=238)
BarVolSyringe2.place(x=930,y=238)

dir = -10
dirr = +10
def sStop():
    global BarVolSyringe1, BarVolSyringe2
    BarVolSyringe1.stop()
    BarVolSyringe2.stop()

def mAspire():
    global BarVolSyringe1, BarVolSyringe2
    sEmpty()
    mGui.after(1000, sUp)
    mGui.after(10000, sStop)
    mGui.after(10010, sFull)

def sUp():
    global BarVolSyringe1, BarVolSyringe2
    BarVolSyringe1.step(dirr)
    BarVolSyringe2.step(dirr)
    mGui.after(10, sUp)

def mDispense():
    global BarVolSyringe1, BarVolSyringe2
    sFull()
    mGui.after(1000, sDown)
    mGui.after(9999, sStop)
    mGui.after(10000, sEmpty)

def sDown():
    global BarVolSyringe1, BarVolSyringe2
    BarVolSyringe1.step(dir)
    BarVolSyringe2.step(dir)
    mGui.after(10, sDown)

def sFull():
    global BarVolSyringe1, BarVolSyringe2
    BarVolSyringe1 = ttk.Progressbar(mGui, orient='vertical', length=78, mode='determinate', value = 100)
    BarVolSyringe2 = ttk.Progressbar(mGui, orient='vertical', length=78, mode='determinate', value = 100)
    BarVolSyringe1.place(x=952,y=238)
    BarVolSyringe2.place(x=930,y=238)

def sEmpty():
    global BarVolSyringe1, BarVolSyringe2
    BarVolSyringe1 = ttk.Progressbar(mGui, orient='vertical', length=78, mode='determinate', value = 0)
    BarVolSyringe2 = ttk.Progressbar(mGui, orient='vertical', length=78, mode='determinate', value = 0)
    BarVolSyringe1.place(x=952,y=238)
    BarVolSyringe2.place(x=930,y=238)

mButtonAs = Button(mGui, text = "Aspire", command = mAspire,
                   fg = 'Black').place(x=110,y=360)
mButtonDis = Button(mGui, text = "Dispense", command = mDispense,
                   fg = 'Black').place(x=160,y=360)

沒有錯誤的python消息,但是當我運行該函數時,它會保持滿狀態,並且永遠不會清空進度欄。 為什么這不起作用?

BarVolSyringe1是未定義sDown ,它是在一個局部變量mDispense 在要共享的功能之外創建欄:

BarVolSyringe1 = ttk.Progressbar(mGui, orient='vertical', length=78, mode='determinate', value = 100)
BarVolSyringe2 = ttk.Progressbar(mGui, orient='vertical', length=78, mode='determinate', value = 100)

def mDispense():
    BarVolSyringe1.place(x=952,y=238)
    BarVolSyringe2.place(x=930,y=238)
    sDown()
    mGui.after(5100, sEmpty)  # Schedule sEmpty only once, not in each sDown call

def sDown():
    BarVolSyringe1.step(dir)
    mGui.after(100, sDown)

def sEmpty():
    BarVolSyringe1.step(-100)
    BarVolSyringe2.step(-100)

編輯:

您不應在函數中分配BarVolSyringe1BarVolSyringe2 它們是全局變量,並且使用BarVolSyringe1 = ...分配值不會重寫全局變量,它只是創建一個具有相同名稱的局部變量,該局部變量會BarVolSyringe1 = ...全局,直到函數結束。 因此,您在函數外部創建的條形圖保留在此處,並使用sDown更新,但是您看不到此內容,因為在sFull中創建的sFull顯示在它們上方。

要解決這個問題:

  • 全局創建變量,並且僅使用其訪問器方法(如BarVolSyringe1.stepBarVolSyringe2.update
  • global聲明放在函數中的全局變量之前:
def sEmpty():
    global BarVolSyringe1, BarVolSyringe2
    BarVolSyringe1 = ttk.Progressbar(mGui, orient='vertical', length=78, 
                                     mode='determinate', value = 0)
    BarVolSyringe2 = ttk.Progressbar(mGui, orient='vertical', length=78, 
                                     mode='determinate', value = 0)
  • 使BarVolSyringe1BarVolSyringe2某些可變對象的內部,例如某些dictlist ,某個類的實例或mGui 然后,您將可以通過該對象在任何地方訪問和更新它們。
mGui.BarVolSyringe1 = ttk.Progressbar(mGui, orient='vertical', length=78, 
                                      mode='determinate', value = 100)
mGui.BarVolSyringe2 = ttk.Progressbar(mGui, orient='vertical', length=78, 
                                      mode='determinate', value = 100)

要么

syringes = {
    'BarVolSyringe1': ttk.Progressbar(mGui, orient='vertical', length=78,
                                      mode='determinate', value = 100),
    'BarVolSyringe2': ttk.Progressbar(mGui, orient='vertical', length=78, 
                                      mode='determinate', value = 100)
}

# Updating examples:
syringes['BarVolSyringe1'].step(dir)
syringes['BarVolSyringe1'] = ttk.Progressbar(mGui, orient='vertical', length=78,
                                             mode='determinate', value = 100),

要么

syringes = [
    ttk.Progressbar(mGui, orient='vertical', length=78,
                    mode='determinate', value = 100),
    ttk.Progressbar(mGui, orient='vertical', length=78,
                    mode='determinate', value = 100),
]

# Updating examples:
syringes[0].step(dir)
syringes[1] = ttk.Progressbar(mGui, orient='vertical', length=78,
                              mode='determinate', value = 100),

我以一種簡單的方式弄清楚了,在python上嘗試一下

import time
import serial
import sys
import os
import tkinter as tk
from tkinter import ttk
from tkinter import *
try:
  import Tkinter
  import ttk
except ImportError:
  import tkinter as Tkinter
  import tkinter.ttk as ttk
import time
mGui = Tk()
mGui.title("GUI")
mGui.geometry('120x450+100+100')
BarVolSyringe1 = ttk.Progressbar(mGui, orient='vertical', length=78, mode='determinate', value = 0)
BarVolSyringe2 = ttk.Progressbar(mGui, orient='vertical', length=78, mode='determinate', value = 0)
BarVolSyringe1.place(x=52,y=38)
BarVolSyringe2.place(x=30,y=38)
SFg=0
SFd=0
SyringeSize = 1000
Sy = SyringeSize/100
SVV_1 = 500
def mAspire():
 global SFg,SFd, BarVolSyringe2,BarVolSyringe1
 if SVV_1<=SyringeSize:
  while 0<=SFg<SVV_1:
    SFg=SFg+1
    SFd=SFd+1
    time.sleep(0.0001)
    BarVolSyringe1.step(1/Sy)
    BarVolSyringe2.step(1/Sy)
    mGui.update()
    print (SFg)
  else:
    mGui.update()
    SFg=0
    print (SFd)
  if (SFd >= SyringeSize):
    BarVolSyringe1.stop()
    BarVolSyringe2.stop()
    BarVolSyringe1 = ttk.Progressbar(mGui, orient='vertical', length=78, mode='determinate', value = 100)
    BarVolSyringe2 = ttk.Progressbar(mGui, orient='vertical', length=78, mode='determinate', value = 100)
    BarVolSyringe1.place(x=52,y=38)
    BarVolSyringe2.place(x=30,y=38)
    SFg=SVV_1
 else:
  messagebox.showerror("ERROR", "Please Select a valve")
mButtonAs = Button(mGui, text = "up", command = mAspire, fg = 'Black').place(x=10,y=60)

def dw():
  global SFg,SFd, BarVolSyringe2,BarVolSyringe1
  SFg=0
  while 0<=SFg<SVV_1:
    SFg=SFg+1
    SFd=SFd-1
    time.sleep(0.0001)
    BarVolSyringe1.step(-(1/Sy))
    BarVolSyringe2.step(-(1/Sy))
    mGui.update()
    print (SFg)
  else:
    mGui.update()
    SFg=0
    print (SFd)
  if (SFd == 0):
    BarVolSyringe1.stop()
    BarVolSyringe2.stop()
    BarVolSyringe1 = ttk.Progressbar(mGui, orient='vertical', length=78, mode='determinate', value = 0)
    BarVolSyringe2 = ttk.Progressbar(mGui, orient='vertical', length=78, mode='determinate', value = 0)
    BarVolSyringe1.place(x=52,y=38)
    BarVolSyringe2.place(x=30,y=38)

mButtonAs = Button(mGui, text = "dw", command = dw, fg = 'Black').place(x=10,y=90)

嘗試此sDown。

def sDown():
    BarVolSyringe1.step(dir)
    BarVolSyringe1.update ()  # Added this.
    mGui.after(1000, sDown)
    mGui.after(5100, sEmpty)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM