How to clear an entry () in tkinter from python - python

In my program, I want to delete the input introduced in the entry() pressing a button that is call "Delete input"
I define a function called SendData(), and I try to use: Entryy.clipboard_clear, and when I test it, it doesn´t do anything.
By the way, this is all of my code:
from tkinter import* #importar el tkinter
App = Tk()
def SendData():
Entryy.clipboard_clear
App.title("HEYYY WASUP")
App.geometry("420x656")
App.config(bg="grey", cursor="cross", relief="sunken", bd="10")
Entryy=Entry()
Entryy.grid(row=0, column=2)
Entryy.config(width=25, font=("Comic sans ms", 10))
botonEnviar = Button(text="Delete input")
botonEnviar.grid(row=0, column=5)
botonEnviar.config(font=("Comic sans ms", 10), command= SendData)
App.mainloop()

Try this:
def SendData():
Entryy.delete(0, END)

Related

Tkinter window swapping

I don't know much about python or Tkinter but my functions aren't firing. No errors but No result either.
from tkinter import *
def root_win():
root = Tk()
root.geometry('700x400+100+100')
root.maxsize(700, 400)
root.minsize(700, 400)
root.title("root")
btn1_root = Button(root, text="ext1", command=lambda: [root.destroy, ext1_win])
btn1_root.place(x=590, y=360, height=30, width=100)
btn1_root.configure(bg="DodgerBlue3", fg="black")
def ext1_win():
ext1 = Tk()
ext1.geometry('700x400+100+100')
ext1.maxsize(700, 400)
ext1.minsize(700, 400)
ext1.title("1")
btn1_ext1 = Button(ext1, text="back", command=lambda: [ext1.destroy, root_win])
btn1_ext1.place(x=590, y=360, height=30, width=100)
btn1_ext1.configure(bg="DodgerBlue3", fg="black")
root_win()
I'm trying to make it so I can hop between different windows to conserve screen space when I start putting together the rest of my project. Tkinter doesn't seem to like all of def root_win(): being a def.
If you use lambda, you need to call the functions inside the body of the lambda.
btn1_ext1 = Button(ext1, text="back", command=lambda: [root.destroy(), ext1_win()])
You also need to call mainloop. With the above code your functions will be called, but they just run to completion and then exit.
def root_win():
root = Tk()
...
root.mainloop()
In Line 12, change this:
btn1_root = Button(root, text="ext1", command=lambda: [root.destroy, ext1_win])
to:
btn1_root = Button(root, text="ext1", command=ext1_win)
Output you can see root.title("root"):
Output you can see ext1.title("1":

How can I append elements inputted by user in a tk.Entry object into a list?

I'm trying to build a very simple program in Python and Tkinter that allows the user to input people's names by keyboard and then a button is commanded to select a person from the list at random and show it in a tk.Label object.
My problem is once I run the root.mainloop(), I can add names to the list but the list does not update with the new names.
This is the main code for the Tkinter to initialize
import tkinter as tk
from tkinter import filedialog, Text
import random
root = tk.Tk()
root.title('Millor persona del moment')
root.geometry("500x200")
root.configure(bg='black')
peopleList = []
tk.Label(root, text="Insertar participante: ",fg="#ff007f", bg='black').grid(row=0)
e1 = tk.Entry(root)
e1.grid(row=0, column=1)
addButton = tk.Button(root, text='Añadir', padx=10, pady=5, fg="#ff007f", bg='black', command=addPerson)
addButton.grid(row=0, column=2)
while peopleList:
turnButton = tk.Button(root, text='Saca Tema', padx=10, pady=5, fg="#ff007f", bg='black', command=wordTurn(peopleList))
turnButton.grid(row=1, column=0)
nom = tk.StringVar()
nom.set('Comencem')
personSpeaking = tk.Label(root, textvariable=nom,fg="#ff007f", bg='black')
personSpeaking.grid(row=1, column=1)
root.mainloop()
And these are the functions I use
def addPerson():
peopleList.append(e1.get())
e1.delete(0,'end')
def wordTurn(peopleList):
person = random.choice(peopleList)
peopleList.remove(person)
nom.set(person)
command=wordTurn(peopleList)) calls the return value of wordTurn when the button is pressed, which is None and should raise an error. Use command=lambda peopleList=peopleList: wordTurn(peopleList)) instead.

I cant get the value from the second textbox in my tkinter application

I'm trying to program a very little app that should start with a window where you put a password, and that works. after that it opens another window and asks for credential that will go into a file (not developed yet!).
My problem is that I can't get the value from the second text widget in the second window. Can someone help me with that?
here's the code:
from tkinter import *
import tkinter as tk
root=Tk()
root.geometry("400x400")
root.title("password test")
def getNewWindow():
root2 = tk.Tk()
root2.geometry("200x200")
root2.title("infos")
textBox2=Text(root2, height=2, width=10)
textBox2.pack()
def retrieve_input2():
inputValue2=textBox2.get("1.0","end-1c")
if inputValue2 == "110604":
pass
buttonCommit=Button(root2, height=1, width=10, text="Commit",
command=lambda: retrieve_input2())
buttonCommit.pack()
def retrieve_input():
inputValue=textBox.get("1.0","end-1c")
if inputValue == "110604":
getNewWindow()
textBox=Text(root, height=2, width=10)
textBox.pack()
buttonCommit=Button(root, height=1, width=10, text="Commit",
command=lambda: retrieve_input())
#command=lambda: retrieve_input() >>> just means do this when i press the button
buttonCommit.pack()
mainloop()

time delay using 'after' method in tkinter works independently

Consider this example
from tkinter import *
history_text_list=[]
window=Tk()
window.geometry("1366x768+1+1")
winner_lbl=Label(window, text="0", fg='red', font=("Comic Sans MS", 96))
winner_lbl.place(x=5, y=5)
a_pixel = PhotoImage(width=1, height=1)
next_btn=Button(window, text="Next", font=("Comic Sans MS", 32), fg='blue', image=a_pixel, height = 70 , width = 130,compound="c")
next_btn.place(x=235, y=70)
history_text = StringVar()
history_label = Label(window, textvariable=history_text, wraplength=850, font=("Comic Sans MS", 16),justify="left",anchor="nw")
history_label.place(x=410, y=5)
def countdown(list_of_numbers):
print('in function list-'+str(list_of_numbers))
winner_lbl['fg'] = 'black'
winner_lbl['text'] = list_of_numbers[0]
list_of_numbers.pop(0)
if list_of_numbers:
window.after(500,countdown,list_of_numbers)
else:
winner_lbl['fg'] = 'red'
return
def MyButtonClicked(self):
one_to_hundred=list(range(1,10))
countdown(one_to_hundred)
history_text_list.append(10)
history_text.set(str(history_text_list))
next_btn.bind('<Button-1>', MyButtonClicked)
window.mainloop()
Here, on pressing next button you will notice that label '0' increments to 9 but 10 gets added in history area quickly though it should happen after countdown function has ended .
What should I do to ensure serial execution ?
Thanks.
The after method returns right away. It does not wait for the timeout to expire. You should place the history_text.set call in your countdown function when the list of numbers is empty.
By the way, you can use if list_of_numbers instead of if len(list_of_numbers)==0 to check if the list is empty.

Tkinter not creating a window?

I am trying to create a window with Tkinter but no window is being created and I am not receiving any error codes?
from tkinter import *
def login_window():
window=Tk()
window.title("Login")
info_lbl = Label(window)
info_lbl.grid(row=0, column=1)
username_lbl = Label(window, text='Username')
username_lbl.grid(row=1, column=1)
username_entry = Entry(window, width=10)
username_entry.grid(row=1, column=2)
password_lbl = Label(window, text='Password')
password_lbl.grid(row=2, column=1)
password_entry = Entry(window, width=10, )
password_entry.grid(row=2, column=2)
ok_button = Button(window, text='Login', command = menu_window)
ok_button.grid(row=3,column = 2,sticky =W)
Any help would be great!
Well I think u should add mainloop() inside your function as well as call your login_window something like this-
from Tkinter import *
def login_window():
window=Tk()
window.title("Login")
mainloop()
login_window()
It looks like you never entered the main Tkinter loop. To display that window, you could add this to the bottom of the function:
window.mainloop()
Take a look at this question and the accepted answer for a bit more information on the Tkinter main loop.

Categories

Resources