目前我正在初学者python类中,制作圣诞卡以学习如何使用tkinter绘制内容。我想弄清楚的是如何制作它以便我的签名直接写入卡本身,而不是输入窗口,并允许我按退格键并删除stuf如果我写错字。然后,当我按下回车键时,它会锁定代码并停止允许用户输入。这是当前可怕的代码。
from tkinter import *
window=Tk()
window.title('At the \'Sell Your Soul!\' shop, batteries are NOT included.')
c=Canvas(window,height=500,width=500)
c.pack()
background=c.create_rectangle(500,500,1,1,fill='red')
line1=c.create_text(250,50,text='Are you tired of being a mere mortal monster?')
titleline=c.create_text(250,10,text='SELL YOUR SOUL HERE!')
line2=c.create_text(250,100,text='Hate having to wait for the \'hero\'s permenant seal against evil\' to break?')
line3=c.create_text(250,150,text='Well, you\'re in luck! Here, at D. Advocates Inc, we\'ll fight for your right to be free!')
line4=c.create_text(250,200,text='All for the low, low price of your eternal soul bound in servitude!')
line5=c.create_text(250,250,text='Sign now, and we\'ll give you an amazing financing rate! 0% for the rest of your \'Life\'!')
line6=c.create_text(250,300,text='You\'ll be resurrected instantly, ready to commit any atrocities and crimes you can think of!')
line7=c.create_text(250,350,text='Revived long after the fall of your evil empire? Don\'t worry, we have that covered, too!')
line9=c.create_text(250,400,text='We\'ll teach you the new language free! That\' right, free! Stop waiting, and sign today!')
line8=c.create_line(250,450,450,450)
xline=c.create_text(255,445,text='X',fill='White')
noguarentee=c.create_text(250,490,text='No guarentees expressed or implied. No refunds, only store credit. Batteries not Included.')
signhere=c.create_text(350,460,text='Sign \'your name\' here.')
signature=c.create_text(350,440,text='your name',fill='white',font=('Edwardian Script ITC',30),state=HIDDEN)
def signing(event):
c.itemconfig(signature,state=NORMAL)
c.itemconfig(xline,state=HIDDEN)
c.bind_all('<KeyPress-x>', signing)
如果我纠正了,我需要输入签名的代码是这样的,对吗?
allowtyping=True
while allowtyping==True:
*allow for typing code here*
def signed(event):
allowtyping=False
c.bind_all('<KeyPress-Enter>', signed)
答案 0 :(得分:0)
你可以尝试这个,我已经创建了一个输入字段并确认用户输入其签名的按钮。当他们按下确认按钮时,它会破坏按钮和输入字段,并将输入的签名放入画布中:
from tkinter import *
window=Tk()
window.title('At the \'Sell Your Soul!\' shop, batteries are NOT included.')
c=Canvas(window,height=500,width=500)
c.pack()
def lockSig():
enteredSig = signhere.get()
confirmSign.destroy()
signhere.destroy()
c.itemconfig(signature, state=NORMAL, text=enteredSig)
background=c.create_rectangle(500,500,1,1,fill='red')
line1=c.create_text(250,50,text='Are you tired of being a mere mortal monster?')
titleline=c.create_text(250,10,text='SELL YOUR SOUL HERE!')
line2=c.create_text(250,100,text='Hate having to wait for the \'hero\'s permenant seal against evil\' to break?')
line3=c.create_text(250,150,text='Well, you\'re in luck! Here, at D. Advocates Inc, we\'ll fight for your right to be free!')
line4=c.create_text(250,200,text='All for the low, low price of your eternal soul bound in servitude!')
line5=c.create_text(250,250,text='Sign now, and we\'ll give you an amazing financing rate! 0% for the rest of your \'Life\'!')
line6=c.create_text(250,300,text='You\'ll be resurrected instantly, ready to commit any atrocities and crimes you can think of!')
line7=c.create_text(250,350,text='Revived long after the fall of your evil empire? Don\'t worry, we have that covered, too!')
line9=c.create_text(250,400,text='We\'ll teach you the new language free! That\' right, free! Stop waiting, and sign today!')
line8=c.create_line(250,450,450,450)
xline=c.create_text(255,445,text='X',fill='White')
noguarentee=c.create_text(250,490,text='No guarentees expressed or implied. No refunds, only store credit. Batteries not Included.')
signhere= Entry(window, text='Sign \'your name\' here.')
signhere.place(x=280,y=430)
confirmSign = Button(window, text='Confirm', command=lockSig)
confirmSign.place(x=280, y=450)
signature=c.create_text(350,440,text='your name',fill='white',font=('Edwardian Script ITC',30),state=HIDDEN)
或者,您可以使用此行(位于按钮创建之上):
window.bind('<Return>',lockSig)
并将lockSig函数编辑为:
def lockSig(event):
enteredSig = signhere.get()
confirmSign.destroy()
signhere.destroy()
c.itemconfig(signature, state=NORMAL, text=enteredSig)
通过这种方式,您可以摆脱按钮,更符合您的要求。
希望这有帮助!