This question already has answers here:
Why isn't this label changing when I use the tkinter config option
(2 answers)
Closed 1 year ago.
I want this label to configure into the text entry after the user enters the text and hits go but the label isn't configuring.
I want the label that says "Hello!" to change into whatever is put in the main entry. I'm looking for an answer written in full code instead of one fixed line.
Here's my code:
import tkinter as tk
root = tk.Tk()
root.attributes('-fullscreen', True)
exit_button = tk.Button(root, text="Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
def answer():
answer_label.config(text=main_entry.get())
entry_frame = tk.Frame(root)
main_entry = tk.Entry(entry_frame, width=100)
main_entry.grid(row=0, column=0)
go_button = tk.Button(entry_frame, text= 'Go!', width=85, command= answer)
go_button.grid(row=1, column=0)
answer_label = tk.Label(text = "Hello!").pack()
entry_frame.place(relx=.5, rely=.5, anchor='center')
root.mainloop()
1.Split tk.Label and pack().
2.Pass the lable.
import tkinter as tk
root = tk.Tk()
root.attributes('-fullscreen', True)
exit_button = tk.Button(root, text="Exit", command = root.destroy)
exit_button.place(x=1506, y=0)
def answer(answer_label):
answer_label.config(text=main_entry.get())
entry_frame = tk.Frame(root)
main_entry = tk.Entry(entry_frame, width=100)
main_entry.grid(row=0, column=0)
answer_label = tk.Label(text = "Hello!")
answer_label.pack()
go_button = tk.Button(entry_frame, text= 'Go!', width=85, command=lambda: answer(answer_label))
go_button.grid(row=1, column=0)
entry_frame.place(relx=.5, rely=.5, anchor='center')
root.mainloop()
Related
I am working on a simple counter app in tkinter. I rigged up some code looking at few tutorial on web. All the functions of a counter are set up. But when it comes to the designing of the app, I want the Count, the Count button, and the reset button to be aligned at the center.
The code is as below
from tkinter import Label, Button, Tk
from tkinter import font
window = Tk()
window.geometry('500x500')
window.title("Counter")
window.count = 0
def increment():
window.count += 1
lbl.configure(text=window.count)
def reset():
window.count = 0
lbl.configure(text=window.count)
lbl = Label(window, text="0", font=("Apple Braille", 60))
lbl.grid(column=0, row=0)
btn1 = Button(window, text="Count", command=increment)
btn1.grid(column=0, row=1)
btn2 = Button(window, text="Reset", command=reset)
btn2.grid(column=1, row=1)
btn1['font'] = btn2['font'] = font.Font(size=30)
window.mainloop()
A Screenshot of my counter app is here
Any help in this aspect will be appreciated.
Thanks,
It is easier to use pack() instead of grid() for your requirement.
lbl = Label(window, text="0", font=("Apple Braille", 60))
lbl.pack()
# frame for the two buttons
frame = Frame(window)
frame.pack()
btn1 = Button(frame, text="Count", command=increment)
btn1.grid(column=0, row=1)
btn2 = Button(frame, text="Reset", command=reset)
btn2.grid(column=1, row=1)
If you want to put at the center of the window:
# frame for the label and buttons
frame = Frame(window)
frame.place(relx=0.5, rely=0.5, anchor="c") # put at center of window
lbl = Label(frame, text="0", font=("Apple Braille", 60))
lbl.grid(row=0, column=0, columnspan=2)
btn1 = Button(frame, text="Count", command=increment)
btn1.grid(column=0, row=1)
btn2 = Button(frame, text="Reset", command=reset)
btn2.grid(column=1, row=1)
I have this code:
root= tk.Tk()
button1 = tk.Button(root, text='Button', command=lambda:print("Click"))
button1.grid(row=1, column=1)
Label = tk.Label(root, text="Text")
Label.grid(row=1, column=2)
root.mainloop()
What I want to get:
Button:(1, 1), Label:(1, 2)
How can I print the items and their destinations in root?
If you just want to get the information about grid, use grid_info() could get the info about the widget.To get all the widgets on root, just use root.winfo_children().
import tkinter as tk
root = tk.Tk()
button1 = tk.Button(root, text='Button', command=lambda:print("Click"))
button1.grid(row=1, column=1)
Label = tk.Label(root, text="Text")
Label.grid(row=1, column=2)
for widget in root.winfo_children():
print(f"{widget.widgetName}:({ widget.grid_info()['row']}, {widget.grid_info()['column']})")
root.mainloop()
Result:
button:(1, 1)
label:(1, 2)
Would this solve it? What you do is create a function that generates a label in root and then give it to the command argument of your button.
import tkinter as tk
root= tk.Tk()
def x():
y = tk.Label(root, text = "Click")
y.grid(row = 2, column = 1)
button1 = tk.Button(root, text='Button', command=x)
button1.grid(row=1, column=1)
Label = tk.Label(root, text="Text")
Label.grid(row = 2, column = 1)
root.mainloop()
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 2 years ago.
So I've been having a weird bug with Python and Tkinter where a message box pops up too early. I don't know what's wrong: it should be working, right?
from tkinter import *
from tkinter import messagebox
with open("passwords.passwords", "a+") as f:
root = Tk()
root.geometry("1000x1000")
root.wm_title("Info Collect")
mylist = []
var1 = StringVar()
var1.set("Application:")
label1 = Label(root, textvariable=var1, height=2)
label1.grid(row=0, column=0)
var2 = StringVar()
var2.set("Password:")
label2 = Label(root, textvariable=var2, height=2)
label2.grid(row=0, column=3)
ID1 = StringVar()
ID2 = StringVar()
box1 = Entry(root, bd=4, textvariable=ID1)
box1.grid(row=0, column=1)
box2 = Entry(root, bd=5, textvariable=ID2)
box2.grid(row=0, column=4)
def get_info():
f.write("{0}: {1} ".format(box1.get(), box2.get()))
def output_info():
messagebox.showinfo(f.read())
buttonA = Button(root, text="Save info", command=get_info, width=8)
buttonA.grid(row=0, column=2)
buttonB = Button(root, text="Output info", command=output_info(), width=8)
buttonB.grid(row=0, column=5)
root.mainloop()
That is all the code, do I have to do anything to it?
Change this
buttonB = Button(root, text="Output info", command=output_info(), width=8)
to
buttonB = Button(root, text="Output info", command=output_info, width=8)
with parenthesis, the function is called and the returned value will be set to command, when you remove parenthesis you pass the function itself to command to be called when the button is clicked
I have created a chat application, in which i use ListBox for showing the chat history. It looks good until I enter a long sting which goes beyond the screen. Is there a way to break the string and show in new line or any other way to show the complete string. I'm new to Tkinter and im not aware of many widgets available.
Here is my sample code
from tkinter import *
class Actions:
def chatUpdate(chat):
chatlist.insert(Actions.chatLast,chat)
Actions.chatLast=Actions.chatLast+1
chatlist.pack( side=LEFT, fill=BOTH)
chatBox.config(command=chatlist.yview)
def callUpdater():
txt=textBox.get()
text_text.set("")
Actions.chatUpdate(txt)
root = Tk()
root.title("Chat App")
frame1 = Frame(root, bd=4)
frame1.pack(side=TOP)
frame2 = Frame(root, bd=4)
frame2.pack(side=TOP)
frame3 = Frame(root, bd=4)
frame3.pack(side=TOP)
# chat box
chatBox = Scrollbar(frame1)
chatBox.pack(side=RIGHT, fill=Y)
chatlist = Listbox(frame1, yscrollcommand = chatBox.set, width=50)
Actions.chatLast=0
Actions.chatUpdate(" ")
# text box
textView = Label(frame2, text="Input: ")
textView.pack(side=LEFT)
text_text = StringVar()
textBox = Entry(frame2, textvariable=text_text, bd=0, width=40, bg="pink")
textBox.pack(side=RIGHT)
# send button
button = Button(frame3, text="Send", fg="black", command=callUpdater)
button.pack(side=TOP)
root.mainloop()
You can replace the Listbox by a Text widget in 'disabled' mode which automatically wraps long lines. You will just need to put the widget back in 'normal' mode each time you insert text:
from tkinter import *
def callUpdater():
text = textBox.get()
textBox.delete(0, 'end')
chat.configure(state='normal')
chat.insert('end', text + '\n')
chat.configure(state='disabled')
root = Tk()
chatBox = Scrollbar(root)
chat = Text(root, wrap='word', state='disabled', width=50,
yscrollcommand=chatBox.set)
chatBox.configure(command=chat.yview)
chat.grid(row=0, columnspan=2, sticky='ewns')
chatBox.grid(row=0, column=2, sticky='ns')
Label(root, text="Input: ").grid(row=1, column=0)
textBox = Entry(root, bd=0, width=40, bg="pink")
textBox.grid(row=1, column=1)
Button(root, text="Send", command=callUpdater).grid(row=2, columnspan=2)
root.mainloop()
By the way, both the Listbox and Text widgets support the index 'end' so you don't have to keep track of how many lines you have inserted.
how do I position my label which says "Question One" in my def new_window() function. As you run it the label is being positioned at the bottom, And i want it to be applied on the top.
from tkinter import *
from tkinter import ttk
#User Interface Code
root = Tk() # Creates the window
root.title("Quiz Game")
def new_window():
newWindow = Toplevel(root)
display = Label(newWindow, width=150, height=40)
message = Label(newWindow, text="Question One", font = ("Arial", "24"))
display.pack()
message.pack()
display2 = Label(root, width=100, height=30, bg='green')
button1 = Button(root, text ="Continue", command=new_window, width=16,
bg="red")
message_label1 = Label(text="A Quiz Game", font = ("Arial", "24"), padx=40,
pady=20)
message_label2 = Label(root, text="Click 'Continue' to begin.",
wraplength=250)
display2.pack()
button1.pack()
message_label1.pack()
message_label2.pack()
root.mainloop() # Runs the main window loop
You are packing in the wrong order. Do not pack display before your message. So just swapping the order will fix the issue.
Here is the code. Replace your def new_window(): with this
def new_window():
newWindow = Toplevel()
message = Label(newWindow, text="Question One", font = ("Arial", "24"))
display = Label(newWindow, width=150, height=40)
message.pack()
display.pack()
pack method just blindly packs the widget into the window. And the next pack will be done below it if there is space. So take care of the order while packing widgets :)