我需要使用小部件条目输入文本,并在输入按钮上启动。
entry = tk.Text(root, width=30, height=1)
entry.bind("<Return>")
entry.pack()
entry.focus()
这里有一个例子,但是我不知道如何把它插入到程序中。
import tkinter as tk
from tkinter import *
root = tk.Tk()`
c = tk.Canvas(root)
c.pack(expand=1, fill=tk.BOTH)
words = 'London is capital of Great Britain.'
words = words.split()
def new_word(i):
if i == len(words):
i = 0
word = words[i]
middle = (len(word)+1)//2
c.itemconfigure(t1, text=word[:middle-1]+' ')
c.itemconfigure(t2, text=word[middle-1:middle])
c.itemconfigure(t3, text=word[middle:])
root.after(100, lambda: new_word(i+1))
t1 = c.create_text(200,100,text='', anchor='e', font=("Courier", 25))
t2 = c.create_text(200,100,text='', anchor='e', font=("Courier", 25), fill='red')
t3 = c.create_text(200,100,text='', anchor='w', font=("Courier", 25))
new_word(0)
root.geometry('400x200+200+200')
root.mainloop()
提前感谢。
创建一个Entry小部件,并将其与"Return"键,调用一个函数,该函数将Entry框中的文本存储为列表中的单词列表,然后该列表将由new_word()
函数使用。
import tkinter as tk
root = tk.Tk()
c = tk.Canvas(root)
c.pack(expand=1, fill=tk.BOTH)
entryWords = tk.StringVar() # a StringVar() for string type variable of tk
words= [] # will store the list of words entered
def storeWords(event): # a function to call when "Enter" key is pressed
global words # the variable will be used by other functions too, so global
words = entryWords.get().split() #Get the text from Entry, and create the list
new_word(0) # call the function here instead of calling below
entry = tk.Entry(root, textvariable=entryWords, width=30) #set the StringVar as its textvariable
entry.bind("<Return>", storeWords) #bind the "Enter" key with the Entry box and call the function "storeWords" when it is pressed
c.create_window(200, 50, window=entry) #place this Entry widget in the canvas
entry.focus()
def new_word(i):
if i == len(words):
i = 0
word = words[i]
middle = (len(word)+1)//2
c.itemconfigure(t1, text=word[:middle-1]+' ')
c.itemconfigure(t2, text=word[middle-1:middle])
c.itemconfigure(t3, text=word[middle:])
root.after(100, lambda: new_word(i+1))
t1 = c.create_text(200,100,text='', anchor='e', font=("Courier", 25))
t2 = c.create_text(200,100,text='', anchor='e', font=("Courier", 25), fill='red')
t3 = c.create_text(200,100,text='', anchor='w', font=("Courier", 25))
root.geometry('400x200+200+200')
root.mainloop()