下面是我创建的一个函数,并将其放入名为last的文件中_函数.py在
from tkinter import*
def new_gui(app,sound_file,mixer):
track=mixer.Sound(sound_file)
def track_toggle():
if ballCheckbutton.get()==1:
track.play(loops=-1)
else:
track.stop()
ballCheckbutton=IntVar()
c1=Checkbutton(app,text="check me out",command=track_toggle,variable=ballCheckbutton)
c1.pack(side=LEFT)
ballScale=DoubleVar()
def ScaleVolume(v):
track.set_volume(ballScale.get())
ballScale.set(track.get_volume())
s1=Scale(app,variable=ballScale,resolution=0.1,command=ScaleVolume,orient=HORIZONTAL,from_=0.0,to=1.0,label="volume")
s1.pack()
这是我用的文件。。调用代码并运行它。。在
^{pr2}$一切正常。。但我的疑问是。。。在
1>;为什么我不能从最后一个函数文件中删除from tkinter import*
。。因为不管怎么说,它在文件的最上面写的是对的。为什么我会得到一个错误,说IntVar()未定义。在
2>;为什么必须将mixer
作为函数中的参数传递?函数不能直接从调用它的文件顶部的import pygame.mixer
继承它吗?
我想说的是。还有一些TKINTER组件正在使用,但我没有将TKINTER作为参数传递。。是吗!那为什么会有这个。。。选择性参数分配??在
我真的很困惑!!!在
Python“import”遵循与Python语言其余部分相同的作用域规则。在第二个文件的顶部添加“import”不会使Tkinter名称空间对最后一个文件可用_函数.py模块。Tkinter也需要进口。在
按照您的编码方式,您需要通过混频器,因为您在第二个文件中修改它:
如果你在上一次重新导入混音器_函数.py,您将获得另一个mixer实例,而不是以前导入的实例。这没有任何选择性,因为您的两个文件都导入了Tkinter名称空间。在
您应该尝试重新考虑此代码,以避免将Tkinter导入两个模块,以及在一个模块中初始化mixer并将其传递给另一个模块。
相关问题 更多 >
编程相关推荐