I’m trying to use the . split() in a def and I don’t know why it’s not working someone can help me?

Asked

Viewed 69 times

2

I’m trying to build a simple raffle script using the Tkinter library.

from tkinter import *
def sorteio():
    from random import choice
    nomes = (ed1.get)
    splitn = nomes.split(" ")
    sort = choice(splitn)
    lb3['text'] = '\033[34mO nome sorteado foi \033[31m{}'.format(sort.title())
janela = Tk()
ed1 = Entry(janela, width=50, bg='green')
bt1 = Button(janela, width=7, bg='green', command=sorteio, text='Sortear')
lb1 = Label(janela, bg='green', text='Digite no campo a baixo\nos nomes a serem sorteados')
lb2 = Label(janela, bg='green', text='Nomes:')
lb3 = Label(janela, bg='green', text='')
ed1.grid(row=2, column=2)
bt1.grid(row=3, column=2)
lb1.grid(row=1, column=2)
lb2.grid(row=2, column=1)
lb3.grid(row=4, column=2)
janela['bg'] = 'green'
janela.geometry('500x500+500+500')
janela.mainloop()

inserir a descrição da imagem aqui

As you can see, the split function did not work, and I do not know how else I can convert the str into list, if anyone knows how to help me I would be very grateful.

I’m new to programming so I don’t really know what the name of each thing is, so I’m sorry if I missed something there...

  • What error appears in?

1 answer

1


Greetings Eric Verschoor,

I made some adjustments to the drawing method. Follow the lines that have been modified and next to them is a comment for you to compare as was before my changes.

nomes = str(ed1.get()) # nomes = (ed1.get)
splitn = nomes.split(" ") # splitn = nomes.split()
sort = choice(splitn) # não alterei
lb3['text'] = 'O nome sorteado foi: ' + sort.title() # simplifiquei

Follow the full code:

from tkinter import *
from random import choice    

def sorteio():    
    nomes = str(ed1.get())
    splitn = nomes.split()
    sort = choice(splitn)
    lb3['text'] = 'O nome sorteado foi: ' + sort.title()

janela = Tk()

ed1 = Entry(janela, width=50, bg='green')
bt1 = Button(janela, width=7, bg='green', command=sorteio, text='Sortear')
lb1 = Label(janela, bg='green', text='Digite no campo a baixo\nos nomes a serem sorteados')
lb2 = Label(janela, bg='green', text='Nomes:')
lb3 = Label(janela, bg='green', text='')

ed1.grid(row=2, column=2)
bt1.grid(row=3, column=2)
lb1.grid(row=1, column=2)
lb2.grid(row=2, column=1)
lb3.grid(row=4, column=2)

janela['bg'] = 'green'
janela.geometry('500x500+500+500')
janela.mainloop()
  • Thank you very much, helped mt, little by little or I will decrease my mistakes. Tmj brother

  • Nice Eric, when you need help I’m at your disposal. Here in this community you can learn a lot every day!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.