Получить переменную из записи tkinter - PullRequest
0 голосов
/ 27 января 2020

Я пытаюсь получить имя персонажа с помощью поля ввода в tkinter.

def create2():
   global created
   label.config(text ='You need to have a name.')
   entry = tk.Entry(storywindow, text = 'Enter your name...')
   entry.place(relx = .01, rely = .89, relheight = .1, relwidth = .69)
   hero.name = entry.get

Как связать пользовательский ввод с переменной? Спасибо

Обновление с моим рабочим кодом:

def stats():
    label.config(text = hero.name + '\n'+ hero.mclass+'\nLevel:    ' + str(hero.level)  )
    label.config(text = 'Hitpoints: '+ str(hero.hp) +'/'+str(hero.max_hp))

def create2():
    label.config(text ='You need to have a name.')
    txt = tk.Entry(storywindow)
    txt.place(relx = .01, rely = .89, relheight = .1, relwidth = .69)

def getname():
     name = txt.get()
     hero.name = name
     txt.destroy()
     submit.destroy()
     stats()

submit = tk.Button(storywindow, text= 'Submit', command = getname)
submit.place(relx = .7, rely = .89, relheight = .1, relwidth = .29)

def create():
    label.config(text ='Please choose a main class.')
    war = tk.Button(storywindow, text = 'Warrior', command = lambda:[warrior(),war.destroy(),mag.destroy(),rog.destroy(),pri.destroy()])
    war.place(relx = .01, rely = .94, relheight = .05, relwidth = .24)
    mag = tk.Button(storywindow, text = 'Mage', command =  lambda:[mage(),war.destroy(),mag.destroy(),rog.destroy(),pri.destroy()])
    mag.place(relx = .26, rely = .94, relheight = .05, relwidth = .24)
    rog = tk.Button(storywindow, text = 'Rogue', command =  lambda:[rogue(),war.destroy(),mag.destroy(),rog.destroy(),pri.destroy()])
    rog.place(relx = .51, rely = .94, relheight = .05, relwidth = .24)
    pri = tk.Button(storywindow, text = 'Priest', command =  lambda:[priest(),war.destroy(),mag.destroy(),rog.destroy(),pri.destroy()])
    pri.place(relx = .76, rely = .94, relheight = .05, relwidth = .23)

1 Ответ

0 голосов
/ 28 января 2020

В функции callback строка data = str(my_entry.get()) - это то, что вы ищете. Я сделал небольшой пример для вас: вы можете написать текст в Entry, затем нажать кнопку, и текст появится в нижней области.

import tkinter as tk

def callback():
    data = str(my_entry.get())
    my_entry.delete("0", "end")
    print(data)
    my_text.delete('1.0', tk.END)
    my_text.insert("insert", data)

root = tk.Tk()

my_entry = tk.Entry(master=root)
my_entry.pack()

my_button = tk.Button(master=root, command=callback, text="Print text")
my_button.pack()

my_text = tk.Text(root, width=10, height=5)
my_text.insert("insert", "Hello !")
my_text.see("end")
my_text.pack()

root.mainloop()
...