我正在尝试清空一个已满的进度条,模拟注射器的行为但由于某种原因无效。
使用按钮我调用mAspire
,调用sEmpty(),然后调用sUp(),然后调用sStop()然后调用sFull(),这样就会创建2个空的进度条,然后用函数&填充它#34;。步骤"然后停止这些进度条,然后创建2个完整的进度条
使用按钮我调用mDispense
,调用sFull(),然后是sDown(),然后调用sStop()然后调用sEmpty(),这样就会创建2个完整进度条,然后用函数&清空它们#34;。步骤"然后停止这些进度条,然后创建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消息,但是当我运行该函数时,它只是保持满,并且永远不会清空进度条。为什么这不起作用?
答案 0 :(得分:1)
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)
修改强>
您不应该在功能中对BarVolSyringe1
和BarVolSyringe2
进行分配。它们是全局变量,并且使用BarVolSyringe1 = ...
分配值不会重写全局变量,它只会创建一个具有相同名称的本地变量,它会影响全局变量直到函数结束。因此,您在函数之外创建的条形图保留在那里并使用sDown
进行更新,但您没有看到这一点,因为在sFull
中创建的条形图显示在它们上面。
要解决这个问题:
BarVolSyringe1.step
,BarVolSyringe2.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)
BarVolSyringe1
和BarVolSyringe2
置于某个可变对象的内部,例如某些dict
,list
,某个类的实例或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),
答案 1 :(得分:0)
我想通了一个简单的方法,在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)
答案 2 :(得分:-1)
尝试使用sDown。
def sDown():
BarVolSyringe1.step(dir)
BarVolSyringe1.update () # Added this.
mGui.after(1000, sDown)
mGui.after(5100, sEmpty)