我试图返回要打印的输入框中的内容,但即使我选择了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\u 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
欢迎您的建议返回条目小部件中的内容,以便通过选择强制按钮进行打印。你知道吗
我认为你做得不对-你在
accept_output()
内执行output()
,所以你不能阻止它。当一切正常时,您必须在output()
中执行accept_output()
。你知道吗代替}甚至可以看起来像普通按钮。你知道吗
Button
您可以使用Checkbutton
,您将不需要函数choosen
和raised_button
。^如果您使用indicatoron=False
,则{相关问题 更多 >
编程相关推荐