tkinter按钮按下部分完成回调函数,然后停止(为什么?),第二次按下完成i

2024-10-03 11:22:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python的tkinter库和python3。你知道吗

我有一个按钮回调函数,如下所示:

def upload(self):
    #turn off periodic function
    self.master.after_cancel(self.afterFuncID)

    print("uploading (someday)...")

    #First, check all inputs to ensure they're ok
    #DEBUGGING
    print(self.mode.get())
    print(self.clickInterval.get())
    print(self.mouseShape.get())
    print(self.text1.get("1.0","end-1c"))
    print(self.text2.get("1.0","end-1c"))

    print("test")

    #turn periodic function back ON
    self.doPeriodically();

由于某种原因,当我按下按钮时,它会在打印“上传(某一天)…”后停止,并且不会执行任何剩余的打印,除非我第二次按下按钮。你知道吗

我将我的按钮链接到自我上传此处的函数:

    uploadButton = tkinter.Button(subframe2Horz, text='Upload', command=self.upload, height=1, width=10, bg="#%02x%02x%02x" % (0, 200, 0), font="Times 12 bold")
    uploadButton.pack(side=tkinter.RIGHT)

我在“上传”函数中引用的周期函数

def doPeriodically(self):
    self.checkBytesAvail()
    self.afterFuncID = self.master.after(200, self.doPeriodically) #repeat call (default 200ms)

这是一个完整的代码示例,可以在您的计算机上重现问题:

更新20160711: 当从空闲状态运行Python时,我的问题是不可再现的。相反,它是可复制的,只有运行Python。(https://www.continuum.io/downloads)我正在windows8.1中用python3.5、windows64位运行Anaconda。

#http://stackoverflow.com/questions/38298220/tkinter-button-press-partially-completes-callback-function-then-stops-why-2
#Gabriel Staples 
#Updated: 10 July 2016 
"""
Helpful Links:
-http://effbot.org/tkinterbook/entry.htm
-http://effbot.org/tkinterbook/pack.htm
-http://effbot.org/tkinterbook/label.htm
-http://stackoverflow.com/questions/23482748/how-to-create-a-hyperlink-with-a-label-in-tkinter
-http://stackoverflow.com/questions/9661854/how-to-create-a-multiline-entry-with-tkinter - using ScrolledText
-http://stackoverflow.com/questions/25753632/tkinter-how-to-use-after-method
--http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.after-method
-http://stackoverflow.com/questions/2400262/how-to-create-a-timer-using-tkinter
-http://stackoverflow.com/questions/14824163/how-to-get-the-input-from-the-tkinter-text-box-widget


"""


import tkinter
import tkinter.messagebox as messagebox
import webbrowser
from tkinter.scrolledtext import ScrolledText
#from tkinter import font

#constants
initialTimeRem = 60 #sec; update this better later
nonTextBytesUsed = 6 #1 for mode, 1 for custom shape, 2 for click pd, 1 each for start of custom URL and custom message
initialBytesRem = 512 - nonTextBytesUsed #bytes 

class App:

    def __init__(self, master):
        #version
        self.version = "0.1.0"

        self.master = master      

        #main frame
        frame = tkinter.Frame(master)
        frame.pack(fill=tkinter.BOTH, expand=1)

        #set mode & click interval INPUTS
        subframe1 = tkinter.Frame(frame)
        subframe1.pack(anchor='w')
        label_setMode = tkinter.Label(subframe1, text='Set mode to: ').pack(side=tkinter.LEFT)
        self.mode = tkinter.IntVar() #device mode
        self.mode.set(1) #set default 
        tkinter.Entry(subframe1, textvariable=self.mode, width=5).pack(side=tkinter.LEFT)
        tkinter.Label(subframe1, text='Click interval (sec): ').pack(side=tkinter.LEFT)
        self.clickInterval = tkinter.IntVar() #sec
        self.clickInterval.set(30) #set default 
        tkinter.Entry(subframe1, textvariable=self.clickInterval, width=5).pack(side=tkinter.LEFT)
        #Mode 1 mouse shape
        subframe_mouseShape = tkinter.Frame(frame)
        subframe_mouseShape.pack(anchor='w')
        label_mouseShape = tkinter.Label(subframe_mouseShape, text='Mode 1 mouse move shape (0=none, 1=cir, 2=square, 3=heart): ').pack(side=tkinter.LEFT)
        self.mouseShape = tkinter.IntVar()
        self.mouseShape.set(1)
        tkinter.Entry(subframe_mouseShape, textvariable=self.mouseShape, width=5).pack(side=tkinter.LEFT)
        #URL & message INPUTS 
        #URL
        tkinter.Label(frame, text='Custom Mode 1 URL/Run Command: ').pack(anchor='w')
        self.customURL = tkinter.StringVar()
        self.customURL.set("ddddddddd")
        self.text1 = ScrolledText(frame, height=3, wrap=tkinter.CHAR)
        self.text1.pack(anchor='w', fill=tkinter.X)
        self.text1.insert(tkinter.END, self.customURL.get())
        #message
        tkinter.Label(frame, text='Custom Mode 1 Message: ').pack(anchor='w')
        self.customMessage = tkinter.StringVar()
        self.customMessage.set("sssssssssssssss")
        self.text2 = ScrolledText(frame, height=10, wrap=tkinter.WORD)
        self.text2.pack(anchor='w', fill=tkinter.X)
        self.text2.insert(tkinter.END, self.customMessage.get())

        subframe2Horz = tkinter.Frame(frame)
        subframe2Horz.pack(side=tkinter.TOP, fill=tkinter.X)

        #upload button
        uploadButton = tkinter.Button(subframe2Horz, text='Upload', command=self.upload, height=1, width=10, bg="#%02x%02x%02x" % (0, 200, 0), font="Times 12 bold")
        uploadButton.pack(side=tkinter.RIGHT)

        self.doPeriodically()

    def doPeriodically(self):
        self.checkBytesAvail()
        self.afterFuncID = self.master.after(200, self.doPeriodically) #repeat call (default 200ms)

    def checkBytesAvail(self):
        val = 1 #do nothing

    def upload(self):
        #turn off periodic function
        self.master.after_cancel(self.afterFuncID)

        print("uploading (someday)...")

        #First, check all inputs to ensure they're ok
        #DEBUGGING
        print(self.mode.get())
        print(self.clickInterval.get())
        print(self.mouseShape.get())
        print(self.text1.get("1.0","end-1c"))
        print(self.text2.get("1.0","end-1c"))

        print("test")

        #turn periodic function back ON
        self.doPeriodically();

    def getVersion(self):
        messagebox.showinfo('Software Version', 'Uploader Version ' + self.version)


root = tkinter.Tk()
root.wm_title("Uploader Tool")
root.geometry("400x500+0+0")
app = App(root)
print("begin")
root.mainloop()

#The code never gets to here until AFTER the GUI is closed (and hence mainloop() exits)
print("end")

Tags: totextselfmasterhttpgettkintermode