生成的单词打印到终端但不是 GUI 条目 - Tkinter

Generated Words Print to Terminal But Not GUI Entry - Tkinter

目的是从列表中随机拉出一个单词并将其显示在 GUI 的屏幕上。当我单击“dammi”按钮时,这些词会显示在终端中,但我无法让它们显示在 GUI 中。我尝试了 Entry 和 Label 都没有成功。

from tkinter import *
import random

# Window

root = Tk()
root.geometry("400x350")
root.title("Passato Remoto")
root.configure(bg="#000000")
root.resizable(False, False)

# Find Verb

verbi = ['Dare', 'Dire', 'Fare', 'Sapere', 'Prendere']

# Dammi Button

def give():
    print(random.choice(verbi))

# Create Buttons

dammi = Button(root, text='Dammi un verbo',
bg='#ffffff',
fg='#000000',
borderwidth=0,
highlightthickness=0,
font=('Helvetica', 16),
height=2,
width=10,
command=give)
dammi.grid(column=0, row=2, pady=50, padx=25)

con = Button(root, text='Coniugazione',
bg='#ffffff',
fg='#000000',
borderwidth=0,
highlightthickness=0,
font=('Helvetica', 16),
height=2,
width=10)
con.grid(column=2, row=2, pady=50, padx=25)

# Put Verb On Screen

verb = Entry(root, text=give(), font=('Helvetica', 40), width=10, bg="#ffffff", fg="#000000")
verb.grid(column=0, columnspan=3, row=1, pady=50, padx=80)

root.mainloop()


最简单的方法可能是使用 StringVar。我已将其添加到问题代码中。

from tkinter import *
import random

root = Tk()
root.geometry("400x350")
root.title("Passato Remoto")
root.configure(bg="#000000")
root.resizable(False, False)
    
# Find Verb
    
verbi = ['Dare', 'Dire', 'Fare', 'Sapere', 'Prendere']
   
# Dammi Button
   
sample = StringVar()           # Added
   
def give():                    # Changed
    sample.set( random.choice(verbi) )
    print( sample.get() )
   
# Create Buttons
   
dammi = Button(root, text='Dammi un verbo',
   bg='#ffffff',
   fg='#000000',
   borderwidth=0,
   highlightthickness=0,
   font=('Helvetica', 16),
   height=2,
   width=10,
   command=give)
dammi.grid(column=0, row=2, pady=50, padx=25)
   
con = Button(root, text='Coniugazione',
   bg='#ffffff',
   fg='#000000',
   borderwidth=0,
   highlightthickness=0,
   font=('Helvetica', 16),
   height=2,
   width=10)
con.grid(column=2, row=2, pady=50, padx=25)
       
# Put Verb On Screen

#                                          changed here
verb = Label(root, font=('Helvetica', 40), textvariable = sample, width=10, bg="#ffffff", fg="#000000")
verb.grid(column=0, columnspan=3, row=1, pady=50, padx=80)
give()   
root.mainloop()