[Tutor] tkinter code executes before function returned

Chris Roy-Smith chris_roysmith at internode.on.net
Sat Apr 14 22:57:22 EDT 2018


Hi,

System: Python 3.6, Ubuntu Linux 17.10

I am trying to get tkinter to return a number from a window, which then 
sets how many times to print a sign.

The code does not wait till the function returns a value, resulting in 
the signcount variable in having a None value, giving an output like 
below. Note that the output "line 64 ###   The required number of signs 
is 5   ###" only occurs after a number is input.

I can use an input statement to get the desired output, but that's not 
good having to go to the terminal to enter the response. How can I get 
the printSign code to wait till getcount() returns it's value?

Any help greatly appreciated.

Regards, Chris Roy-Smith

chris at chris-X451MA:~/Scripts/python3/dvms$ ./debug1.py
line 27 ###   required sign count for D is None   ###
Exception in Tkinter callback
Traceback (most recent call last):
   File "/usr/lib/python3.6/tkinter/__init__.py", line 1702, in __call__
     return self.func(*args)
   File "./debug1.py", line 28, in printSign
     for x in range(signcount):
TypeError: 'NoneType' object cannot be interpreted as an integer
line 64 ###   The required number of signs is 5   ###


Code:

#!/usr/bin/python3
from tkinter import *
import os
from reportlab.lib.units import cm
from reportlab.lib.pagesizes import A4
from reportlab.pdfgen import canvas
from reportlab.lib.utils import ImageReader

def printSign():
     global gc, packages, rows
     myCanvas = canvas.Canvas("Signs.pdf", pagesize=A4)
     width, height = A4 #keep for
     myCanvas.rotate(90)
     myCanvas.setFillColorRGB(0,0,0)
     myCanvas.setFont("Helvetica-Bold", 400)
     TopMargin=-20
     LeftMargin=1
     Width=14
     Height=19
     VertPos=-15
     Bottom=-1
     a=[" " for i in range(rows)]
     i=0
     sign=0
     for line in packages:
         signcount=getcount(line[1])
         print('line 27 ###   required sign count for {} is {} 
###'.format(line[1], str(signcount)))
         for x in range(signcount):
             #draw rectangle
             myCanvas.rect(LeftMargin*cm+Width*sign*cm, TopMargin*cm, 
Width*cm, Height*cm, stroke=0, fill=1)
myCanvas.drawCentredString((LeftMargin+(0.5*Width))*cm+(Width*sign)*cm, 
VertPos*cm, line[0])
             if sign==1:
                 myCanvas.showPage()
                 sign=0
                 myCanvas.rotate(90)
                 i+=1
             else:
                 sign+=1
                 i+=1

     myCanvas.showPage()
     myCanvas.save()
     if os.name == "posix":
         os.popen("evince %s" % ("Signs.pdf"))
     if os.name == "nt":
         os.startfile('Signs.pdf')

def getcount(SignText):
     global gc,e
     gc=Toplevel(master)
     MsgText='How many copies of {} do you want to print?'.format(SignText)
     Label(gc, text=MsgText).grid(row=0, column=0, sticky=(W,E))
     e = Entry(gc)
     e.grid(row=0, column=1)
     Button(gc, text='Okay', command=ReturnCount).grid(row=1, column=0, 
sticky=(W,E))
     Button(gc, text='Cancel', command=gc.destroy).grid(row=1, column=1, 
sticky=(W,E))

def ReturnCount():
     global gc,e
     b0=e.get()
     if b0 == None:
         b0=0
     gc.destroy()
     print('line 64 ###   The required number of signs is {} 
###'.format(b0))
     return b0

master = Tk()
master.title("Testing")
packages = [[0,'D','drill'],[1,'J','Jointer'],[2,'B','Bandsaw']]
rows = 3
b2 = Button(master, text="Print Signs", command=printSign).grid(row=4, 
column=0, sticky=(W,E), padx=5, pady=5)
b3 = Button(master, text="Quit", command=master.destroy).grid(row=4, 
column=3, sticky=(W,E))

master.mainloop()



More information about the Tutor mailing list