我的按钮将启动并停止从外部模块调用的功能

发布于 2025-01-20 20:42:29 字数 1191 浏览 0 评论 0原文

通过仅放置R.Loop(),该功能在GUI内调用时起作用。但是,它不会单击按钮。我尝试使用.configure,但这不起作用。该模块在源代码内部也可以单独工作。我以为我在某个地方听说TKINTER不喜欢Globals,但我不确定。

    #creating a global variable for running loop
    global Run
    Run=False
    
    #begin GUI CODE
    
    main = tk.Tk()
    main.title('REGENERATOR')
    main.geometry('800x800')    
    
    ...
    
    #THIS IS THE CALL OF FUNCTION SETUP
    
    running_setup=R.setup()
    
    #Funtion to initialize the motor
    
    def motor():    
        if Run(True):
            R.loop()
    
    #This is the start button
    
    def startmotor():   #Function to start motor
        global Run
        Run = True
        #Button_run.configure()
    
    Button_run=Button(frame5, text="run", command=lambda:startmotor, bg="white", fg="black", padx=5, pady=5, font=("Times", 16))
    Button_run.grid(row="2", column="0")        
    
    #This is the stop button
    
    def stopmotor():
        global Run
        Run = False
        #Button_stop.comfigure()
    
    Button_stop=Button(frame5, text="stop", command=lambda:stopmotor,  bg="white", fg="black", padx=5, pady=5, font=("Times", 16))
    Button_stop.grid(row="2", column="1")

The function works when called inside the GUI by just placing R.loop(). However, it doesn't run with a button click. I've tried using .configure, but that doesn't work. The module works fine on its own inside the source code as well. I thought I heard somewhere that Tkinter doesn't like globals, but I'm not sure.

    #creating a global variable for running loop
    global Run
    Run=False
    
    #begin GUI CODE
    
    main = tk.Tk()
    main.title('REGENERATOR')
    main.geometry('800x800')    
    
    ...
    
    #THIS IS THE CALL OF FUNCTION SETUP
    
    running_setup=R.setup()
    
    #Funtion to initialize the motor
    
    def motor():    
        if Run(True):
            R.loop()
    
    #This is the start button
    
    def startmotor():   #Function to start motor
        global Run
        Run = True
        #Button_run.configure()
    
    Button_run=Button(frame5, text="run", command=lambda:startmotor, bg="white", fg="black", padx=5, pady=5, font=("Times", 16))
    Button_run.grid(row="2", column="0")        
    
    #This is the stop button
    
    def stopmotor():
        global Run
        Run = False
        #Button_stop.comfigure()
    
    Button_stop=Button(frame5, text="stop", command=lambda:stopmotor,  bg="white", fg="black", padx=5, pady=5, font=("Times", 16))
    Button_stop.grid(row="2", column="1")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文