我正在使用Python的tkinter库,使用Python 3。
我有一个按钮回调函数,如下所示:
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();
出于某种原因,当我按下按钮时,它会在打印后停止并上传(某天)...",除非我按下按钮,否则不会执行任何其他打印第二次。
我将我的按钮链接到self.upload函数:
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)
doPeriodically函数我参考"上传"功能
def doPeriodically(self):
self.checkBytesAvail()
self.afterFuncID = self.master.after(200, self.doPeriodically) #repeat call (default 200ms)
以下是在您的计算机上重现问题的完整代码示例:
更新20160711: 从IDLE运行Python时,我的问题是不可重现的。相反,它只有在运行Anaconda时才可重现。 (https://www.continuum.io/downloads)我在Windows 8.1中使用Python 3.5,Windows 64位运行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")