Am trying to return the content in my entry box to be printed but the messagebox pops up even when i select the tkinter raised buttons alerting me to select the compulsory button.
Challenges:
when i select the compulsory button before selecting the print button the message box still pops up.
if i don't type in the entry widget the message for it pops up to alert me but but select button also pops up twice instead of only message box to alert me. have passed all the function to the print button too. My compulsory button is to be raised when it is selected then return to normal state after executing the command.
import tkinter as tk
from tkinter import messagebox
import win32print, win32ui, win32con
def output():
global choosed
now = new.get()
if not choosed:
messagebox.showerror("error", "select the buttons")
elif len(now) == 0:
messagebox.showerror("error", "entry empty")
else:
return (now)
def accept_output():
dc = win32ui.CreateDC()
printername = win32print.GetDefaultPrinter()
dc.CreatePrinterDC(printername)
dc.SetMapMode(win32con.MM_TWIPS)
scale_factor = 20
dc.StartDoc('Win32print ')
pen = win32ui.CreatePen(0, int(scale_factor), 0)
dc.SelectObject(pen)
font = win32ui.CreateFont({
"name": "Lucida Console",
"height": int(scale_factor * 10),
"weight": 400,
})
dc.SelectObject(font)
dc.TextOut(scale_factor * 72,-1 * scale_factor * 72, output())# to
receive the content in the entry widget
dc.EndDoc()
def raised_button(button_object):
global choosed
if choosed: # previously clicked
choosed.configure(relief=tk.RAISED, state=tk.ACTIVE)
choosed = button_object
button_object.configure(relief=tk.SUNKEN, state=tk.DISABLED)
def stop():
global choosed
choosed = None
lot1.configure(relief=tk.RAISED, state=tk.ACTIVE)
root = tk.Tk()
root.geometry("400x400")
new = tk.StringVar()
en = tk.Entry(root, textvariable=new).pack()
choosed = False
lot1 = tk.Button(root, text="compulsory to select")
lot1.configure(command=lambda button_object=lot1:
raised_button(button_object))
lot1.pack(side="left")
B=tk.Button(root, text="print number", command=lambda:[output(), stop(),
accept_output()])
B.place(x=150, y=300)
root.mainloop()
I included output() function on top of accept_output() i still got the same error.
def accept_output():
global choosed
now = new.get()
if not choosed:
messagebox.showerror("error", "select one of the buttons")
elif len(now) == 0:
messagebox.showerror("error", "entry empty")
else:
return (now) # when i add the output here it gives select button
welcome your suggestions to return the content in the entry widget for it be printed by selecting the compulsory button.
I think you do it in wrong way - you execute output()
inside accept_output()
so you can't stop it. You have to execute accept_output()
in output()
when all is OK.
import tkinter as tk
from tkinter import messagebox
import win32print, win32ui, win32con
def output():
global choosed
text = new.get()
if not choosed:
messagebox.showerror("error", "select the buttons")
elif len(text) == 0:
messagebox.showerror("error", "entry empty")
else:
accept_output(text)
choosed = None
lot1.configure(relief=tk.RAISED, state=tk.ACTIVE)
def accept_output(text):
print("printing ...", text)
dc = win32ui.CreateDC()
printername = win32print.GetDefaultPrinter()
dc.CreatePrinterDC(printername)
dc.SetMapMode(win32con.MM_TWIPS)
scale_factor = 20
dc.StartDoc('Win32print ')
pen = win32ui.CreatePen(0, int(scale_factor), 0)
dc.SelectObject(pen)
font = win32ui.CreateFont({
"name": "Lucida Console",
"height": int(scale_factor * 10),
"weight": 400,
})
dc.SelectObject(font)
dc.TextOut(scale_factor * 72,-1 * scale_factor * 72, text) # <- text
dc.EndDoc()
def raised_button(button_object):
global choosed
# function will be not accesible if you use state=tk.DISABLED
# so I remove state=tk.DISABLED
if choosed: # previously clicked
choosed.configure(relief=tk.RAISED)
choosed = None
else:
button_object.configure(relief=tk.SUNKEN)
choosed = button_object
# --- main ---
choosed = False
root = tk.Tk()
new = tk.StringVar()
tk.Entry(root, textvariable=new).pack()
lot1 = tk.Button(root, text="Compulsory to select")
lot1.configure(command=lambda button_object=lot1:raised_button(button_object))
lot1.pack()
tk.Button(root, text="Print number", command=output).pack()
root.mainloop()
Instead of Button
you could use Checkbutton
and you will no need function choosen
and raised_button
. Checkbutton
can even looks like normal button if you use indicatoron=False
.
import tkinter as tk
from tkinter import messagebox
import win32print, win32ui, win32con
def output():
text = new.get()
if not option1.get() and not option2.get():
messagebox.showerror("error", "select options")
elif len(text) == 0:
messagebox.showerror("error", "entry empty")
else:
accept_output(text)
option1.set(False)
option2.set(False)
def accept_output(text):
print("printing ...", text)
dc = win32ui.CreateDC()
printername = win32print.GetDefaultPrinter()
dc.CreatePrinterDC(printername)
dc.SetMapMode(win32con.MM_TWIPS)
scale_factor = 20
dc.StartDoc('Win32print ')
pen = win32ui.CreatePen(0, int(scale_factor), 0)
dc.SelectObject(pen)
font = win32ui.CreateFont({
"name": "Lucida Console",
"height": int(scale_factor * 10),
"weight": 400,
})
dc.SelectObject(font)
dc.TextOut(scale_factor * 72,-1 * scale_factor * 72, text) # <- text
dc.EndDoc()
# --- main ---
root = tk.Tk()
new = tk.StringVar()
tk.Entry(root, textvariable=new).pack()
option1 = tk.BooleanVar(value=False)
tk.Checkbutton(root, text="Compulsory to select (option1)", var=option1).pack()
option2 = tk.BooleanVar(value=False)
tk.Checkbutton(root, text="Compulsory to select (option2)", var=option2, indicatoron=False).pack()
tk.Button(root, text="Print number", command=output).pack()
root.mainloop()