时间:2018-03-22 17:33:35

标签: python

我通过输入" python filename.py"来运行脚本。在Anaconda提示。如何在不退出的情况下运行文件? (例如,引入一些函数定义,然后使用这些定义在提示中执行操作)

提前致谢。

1 个答案:

答案 0 :(得分:4)

使用

from pygame import *
from tkinter import *  # Change to "from Tkinter import *" for Python 2.x.

class PlayController(object):
    def __init__(self, mixer, music_filename, polling_delay):
        self.mixer = mixer
        self.music_filename = music_filename
        self.polling_delay = polling_delay  # In milliseconds.
        self.playing = False

    def play(self):
        if self.playing:
            self.stop()
        self.mixer.music.load(self.music_filename)
        self.mixer.music.play(-1)  # -1 means to loop indefinitely.
        self.playing = True
        root.after(self.polling_delay, self.check_status)  # Start playing check.

    def stop(self):
        if self.playing:
            self.mixer.music.stop()
            self.playing = False

    def check_status(self):
        if self.playing:
            print('playing')
        root.after(self.polling_delay, self.check_status)  # Repeat after delay.

root = Tk()
root.geometry("1000x200")
root.title("Sampler")
mixer.init()

play_control = PlayController(mixer, 'tone.wav', 1000)
Button(root, text="Play", command=play_control.play).pack()
Button(root, text="Stop", command=play_control.stop).pack()

mainloop()

它完全符合您的要求:)