2015-12-04 5 views
-1

В настоящее время я нахожусь в классе python для начинающих, создавая рождественские открытки, чтобы узнать, как использовать tkinter для рисования материала. Я хочу выяснить, как сделать так, чтобы моя подпись была написана непосредственно в самой карте, а не во входном окне, и позвольте мне ударить backspace и удалить stuf, если я опечатку. Затем, когда я нажимаю кнопку ввода, он блокирует код и останавливает доступ к пользователю. Вот текущий, отвратительный код.Обновление текста в реальном времени в python

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

Предоставить дополнительную информацию и изменить свой вопрос. Прямо сейчас, это очень запутанно! – python

+0

Как вы ожидаете узнать, когда пользователь будет подписаться? Когда они нажимают кнопку ввода? Нажмите кнопку? Пауза в течение X секунд? –

+0

Как только они нажимают кнопку ввода, подпись завершается. – Muroxxas

ответ

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) 

Таким образом, вы сможете избавиться от кнопки, и более того, что вы просили.

Надеюсь, это поможет!

Смежные вопросы