我试图在我的输入框中返回要打印的内容,但即使我选择了tkinter凸起按钮提示我选择强制按钮,也会弹出消息框。
挑战:
当我在选择打印按钮之前选择强制按钮时,消息框仍然会弹出。
如果我没有在条目小部件中输入,则弹出的消息会提醒我,但是选择按钮也弹出两次而不是仅显示消息框以提醒我。 已将所有功能传递给打印按钮。选中后,我的必修按钮被抬起,然后在执行命令后返回正常状态。
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()
我在accept_output()之上包含了output()函数,我仍然遇到了同样的错误。
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
欢迎您提出建议,在条目小部件中返回内容,方法是选择强制按钮进行打印。
答案 0 :(得分:0)
我认为你做错了 - 你在output()
内执行accept_output()
所以你无法阻止它。当一切正常时,您必须在accept_output()
中执行output()
。
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()
您可以使用Button
代替Checkbutton
而不需要choosen
和raised_button
。如果您使用Checkbutton
,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()