进度条不会下降 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 个完整进度条,然后使用函数“.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 有错误的消息,但是当我 运行 函数时它只是保持完整并且从不清空进度条。为什么这不起作用?
sDown 试试这个。
def sDown():
BarVolSyringe1.step(dir)
BarVolSyringe1.update () # Added this.
mGui.after(1000, sDown)
mGui.after(5100, sEmpty)
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
、某些 class 或 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)
我正在尝试清空已满的进度条,模拟注射器的行为,但由于某种原因无法正常工作。
我调用了一个按钮 mAspire
,它调用了 sEmpty(),然后是 sUp(),然后是 sStop(),然后是 sFull(),所以它会创建 2 个空进度条,然后用函数“.step”然后停止这些进度条然后创建 2 个完整的进度条
使用我调用的按钮 mDispense
,它调用 sFull(),然后是 sDown(),然后是 sStop(),然后是 sEmpty(),因此它将创建 2 个完整进度条,然后使用函数“.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 有错误的消息,但是当我 运行 函数时它只是保持完整并且从不清空进度条。为什么这不起作用?
sDown 试试这个。
def sDown():
BarVolSyringe1.step(dir)
BarVolSyringe1.update () # Added this.
mGui.after(1000, sDown)
mGui.after(5100, sEmpty)
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
、某些 class 或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)