我正在尝试从运行在python上的tkinter中的从属窗口传递一个值,而我无法理解它。这是我到目前为止的代码:
#!/usr/bin/env python3
import time
import pigpio
from tkinter import *
GPIO=21
GPIO2=16
GPIO3=20
pi = pigpio.pi()
if not pi.connected:
exit()
pi.set_mode(GPIO,pigpio.OUTPUT)
pi.set_mode(GPIO2,pigpio.OUTPUT)
gv=int()
def tx_pulses(pi, GPIO, hertz, num, pulse_len=1):
motSpeed = int((1/(dscale.get()))*2000)
print(motSpeed)
pulse_len = motSpeed
assert num < 65536
num_low = num % 256
num_high = num // 256
wf = []
wf.append(pigpio.pulse(1<<GPIO, 0, pulse_len))
wf.append(pigpio.pulse(0, 1<<GPIO, pulse_len))
pi.wave_add_generic(wf)
wid = pi.wave_create()
if wid >= 0:
pi.wave_chain([255, 0, wid, 255, 1, num_low, num_high])
while pi.wave_tx_busy():
time.sleep(0.1)
pi.wave_delete(wid)
def runMotor():
motRevs = int(((scale.get())*400)*5.08)
print(motRevs)
pi.write(GPIO2,0)
pi.write(GPIO3,1)
tx_pulses(pi, GPIO, 1000, motRevs) # 250 pulses @ 1000 Hz
pi.write(GPIO3,0)
tx_pulses(pi, GPIO, 1000, motRevs) # 2391 pulses @ 5000 Hz
pi.write(GPIO2,1)
def setUp():
speedWin = Toplevel()
speedWin.geometry('800x600')
speedWin.title('Stepper Control')
label = Label(speedWin, text='Inches of Travel')
label.place(x=350, y=100)
scale = Scale(speedWin, from_=0.0, to=15.0 ,orient=HORIZONTAL,length=600, resolution=0.1, tickinterval=1)
scale.place(x=100, y=125)
dlabel = Label(speedWin, text='Speed')
dlabel.place(x=370, y=200)
dscale = Scale(speedWin, from_=1, to=10 ,orient = HORIZONTAL,length = 600, tickinterval=1)
dscale.place(x=100, y=225)
button = Button(speedWin, text='Apply Values', command=runMotor, padx = 20, pady = 20)
button.place(x=350, y=350)
button3 = Button(speedWin, text='Back To Main Window', command=speedWin.destroy, padx=20, pady=20)
button3.place(x=500, y=350)
speedWin.mainloop()
mainWin = Tk()
mainWin.geometry('800x600')
mainWin.title('Main Window')
button2 = Button(mainWin, text='Go To Setup', command=setUp, padx=20, pady=20)
button2.place(x=350, y=350)
mainWin.mainloop()
pi.stop()
当我按下“应用值”按钮时,出现错误,指出未定义speedWin。我看了看,但找不到答案或一些示例代码。总计新来的,所以请保持温和!
答案 0 :(得分:0)
您应该搜索SO以查找用tkinter
标记的有关组织tkinter代码的问题。不要将下面的代码视为一个很好的例子,只是运行代码的一个简单示例。
我在代码中看到的主要问题是保留创建的小部件的变量是setUp
函数的本地变量。您可以将它们设为全局变量,并结合使用global
关键字从多个函数中访问它们。这有点难看,因为你可能会开始增加全球变量的数量,你需要的一切都变成了全局变量。
但是你也可以为你的第二个窗口定义一个类,比如ControlPanel
并将访问器函数作为该类的方法,并将小部件保存在变量成员中。如果函数需要引用ControlPanel
小部件,您可以在类外部的代码中说cp.widget_name
cp
是ControlPanel
实例对象,cp = ControlPanel()
或在ControlPanel
类代码中,您可以通过self.widget_name
引用它们。对于小部件或您想要的任何其他变量。
setUp
中的代码已转移到ControlPanel __init__
,后者在创建时初始化ControPanel对象。请注意,类(方法)中的函数现在需要self
参数,而不是其他函数。
不要两次调用mainloop
,mainloop()
之后的代码只会在tkinter终止后运行。
试试这个重新组织的代码:
#!/usr/bin/env python3
import time
import pigpio
from tkinter import *
GPIO=21
GPIO2=16
GPIO3=20
pi = pigpio.pi()
if not pi.connected:
exit()
pi.set_mode(GPIO,pigpio.OUTPUT)
pi.set_mode(GPIO2,pigpio.OUTPUT)
gv=int()
class ControlPanel(Toplevel):
def __init__(self, *args, **kwargs):
super().__init__(args, kwargs)
self.geometry('800x600')
self.title('Stepper Control')
self.label = Label(self, text='Inches of Travel')
self.label.place(x=350, y=100)
self.scale = Scale(self, from_=0.0, to=15.0 ,orient=HORIZONTAL,length=600, resolution=0.1, tickinterval=1)
self.scale.place(x=100, y=125)
self.dlabel = Label(self, text='Speed')
self.dlabel.place(x=370, y=200)
self.dscale = Scale(self, from_=1, to=10 ,orient = HORIZONTAL,length = 600, tickinterval=1)
self.dscale.place(x=100, y=225)
self.button = Button(self, text='Apply Values', command=self.runMotor, padx = 20, pady = 20)
self.button.place(x=350, y=350)
self.button3 = Button(self, text='Back To Main Window', command=self.destroy, padx=20, pady=20)
self.button3.place(x=500, y=350)
self.update()
def tx_pulses(self, pi, GPIO, hertz, num, pulse_len=1):
motSpeed = int((1/(self.dscale.get()))*2000)
print(motSpeed)
pulse_len = motSpeed
assert num < 65536
num_low = num % 256
num_high = num // 256
wf = []
wf.append(pigpio.pulse(1<<GPIO, 0, pulse_len))
wf.append(pigpio.pulse(0, 1<<GPIO, pulse_len))
pi.wave_add_generic(wf)
wid = pi.wave_create()
if wid >= 0:
pi.wave_chain([255, 0, wid, 255, 1, num_low, num_high])
while pi.wave_tx_busy():
time.sleep(0.1)
pi.wave_delete(wid)
def runMotor(self):
motRevs = int(((self.scale.get())*400)*5.08)
print(motRevs)
pi.write(GPIO2,0)
pi.write(GPIO3,1)
self.tx_pulses(pi, GPIO, 1000, motRevs) # 250 pulses @ 1000 Hz
pi.write(GPIO3,0)
self.tx_pulses(pi, GPIO, 1000, motRevs) # 2391 pulses @ 5000 Hz
pi.write(GPIO2,1)
mainWin = Tk()
mainWin.geometry('800x600')
mainWin.title('Main Window')
button2 = Button(mainWin, text='Go To Setup', command=ControlPanel, padx=20, pady=20)
button2.place(x=350, y=350)
mainWin.mainloop()
pi.stop()