I want that to print value that one enters in input box gets printed, and later I'll use it with SQL, but when I run this, it prints with None in terminal.
from tkinter import *
from tkinter import ttk
window=Tk()
def submit():
label.config(text=""+text.get(1.0, "end-1c"))
pri = label.config(text=""+text.get(1.0, "end-1c"))
print(str(pri))
# Add a text widget
text=Text(window, width=80, height=15)
text.insert(END, "")
text.pack()
# Create a button to get the text input
b=ttk.Button(window, text="Print", command=submit)
b.pack()
# Create a Label widget
label=Label(window, text="", font=('Calibri 15'))
label.pack()
window.mainloop()
Related
Hello I'm a few weeks into python and I'm now starting learning tkinter. The button should have the text Say hello and when the user clicks the button, the bottom label should display the name with Hi in front of it. However I cannot get the label to display "Hi {name}". Could someone help me?
from tkinter import *
from tkinter.ttk import *
def process_name():
"""Do something with the name (in this case just print it)"""
global name_entry
print("Hi {}".format(name.get()))
def main():
"""Set up the GUI and run it"""
global name_entry
window = Tk()
name_label = Label(window, text='Enter name a name below:')
name_label.grid(row=0, column=0)
name_entry = Entry(window)
name_entry.grid(row=1, column=3)
button = Button(window, text='Say hello', command=process_name, padding=10)
button.grid(row=1, column=0, columnspan=2)
window.mainloop()
main()
I've tried using set() but it doesn't display.
Thank you.
This should work for your needs. Make sure to read the code comments to understand what I did properly.
Few points though,
Not recommended to use wildcard import like from tkinter import * . The reason is simple. both tkinter and tkinter.ttk have common classes and functions like Button, Label and more. It becomes ambiguous for interpreter to decide which ones to use.
use .config() or .configure() to update labels, buttons, entries or text widgets in tkinter. Like I did in code below.
Your code modified
from tkinter import *
from tkinter.ttk import *
def process_name():
"""Do something with the name (in this case just print it)"""
global name_entry # this will print hi {name} to terminal.
print("Hi {}".format(name_entry.get()))
global nameLabel # to change'the label with hi{name} text below the button.
nameLabel.configure(text=f'Hi {name_entry.get()}')
def main():
"""Set up the GUI and run it"""
global name_entry, nameLabel
window = Tk()
name_label = Label(window, text='Enter name a name below:')
name_label.grid(row=0, column=0)
name_entry = Entry(window)
name_entry.grid(row=1, column=3)
button = Button(window, text='Say hello', command=process_name, padding=10)
button.grid(row=1, column=0, columnspan=2)
# I defined a label BELOW the button to show how to change
nameLabel = Label(window, text=' ') # an empty text Label
nameLabel.grid(row=2)
window.mainloop()
main()
I’m working on a project that uses tkinter and the buttons. So lets say my window has a text 'hi' and a button that displays 'bye' when I click it. When I click it 'hi' is replaced with 'bye', but I want to append 'bye'. How do I stop it from replacing my existing text?
This is my code:
from tkinter import *
window = Tk()
window.title("Welcome ")
window.geometry('1000x200')
lbl = Label(window, text="Hi")
lbl.grid(column=0, row=0)
def click():
lbl.configure(text="bye")
btn = Button(window, text="click here", command=click)
btn.grid(column=1, row=0)
window.mainloop()
You can use cget to get the content, then update it as you wish; for instance:
def click():
lbl.configure(text=lbl.cget('text')+" bye")
I am trying to place a greyed out background text inside a textbox, that disappears when someone begins to type. I have tried overlaying a label onto the textbox, but I could not get it to work. Here is some code I am running for the text box
root = tk.Tk()
S = tk.Scrollbar(root)
T = tk.Text(root, height=70, width=50)
S.pack(side=tk.RIGHT, fill=tk.Y)
T.pack(side=tk.LEFT, fill=tk.Y)
S.config(command=T.yview)
T.config(yscrollcommand=S.set)
root.protocol("WM_DELETE_WINDOW", stop)
tk.mainloop()
How can I put in the background text?
Using a callback function you can remove the default text and change the foreground colour
import tkinter as tk
root = tk.Tk()
e = tk.Entry(root, fg='grey')
e.insert(0, "some text")
def some_callback(event): # must include event
e.delete(0, "end")
e['foreground'] = 'black'
# e.unbind("<Button-1>")
e.unbind("<FocusIn>")
return None
# e.bind("<Button-1>", some_callback)
e.bind("<FocusIn>", some_callback)
e.pack()
root.mainloop()
You are probably talking about placeholders , tkinter does not have placeholder attribute for Text widget, but you do something similar with Entry widget . You have to do it manually, by binding an event with the Text widget.
text = tk.StringVar()
text.set('Placeholder text')
T = tk.Entry(root, height=70, width=50, textvariable = text)
def erasePlaceholder(event):
if text.get() == 'Placeholder text':
text.set('')
T.bind('<Button-1>', erasePlaceholder)
Try to ask if you face any issues.
I'm making a simple word processor and I want to be able to change the font/style (or whatever it's called) of only the text that I highlight.
I can't say what I've tried because I don't even know where to start.
from tkinter import *
# window setup
tk = Tk()
# main textbox
textbox = Text(tk)
textbox.configure(width=85,height=37)
textbox.grid(column=0,row=0,rowspan=500)
# bold
def bold():
textbox.config(font=('Arial',10,'bold'))
bBut = Button(tk,text='B',command=bold)
bBut.configure(width=5,height=1)
bBut.grid(column=0,row=0)
tk.mainloop()
I can change the entire text to bold/italic/etc. but I want to be able to specify parts of it.
It use tags to assign color or font to text
import tkinter as tk
def set_bold():
try:
textbox.tag_add('bold', 'sel.first', 'sel.last')
except Exception as ex:
# text not selected
print(ex)
def set_red():
try:
textbox.tag_add('red', 'sel.first', 'sel.last')
except Exception as ex:
# text not selected
print(ex)
root = tk.Tk()
textbox = tk.Text(root)
textbox.pack()
textbox.tag_config('bold', font=('Arial', 10, 'bold'))
textbox.tag_config('red', foreground='red')
button1 = tk.Button(root, text='Bold', command=set_bold)
button1.pack()
button2 = tk.Button(root, text='Red', command=set_red)
button2.pack()
root.mainloop()
I made a form in Tkinter but when it is anwered I need the information to concatenate to a textbox.
Let's say I select I live in Chicago, how do I send that information to the scrolled text when I press the button "send"?
lblCd= Label(ventana,text="Location:",font= ("Arial", 20),
fg="blue", bg="white").place(x=70,y=400)
combo['values']=("Chicago","NY", "Texas")
combo.current(1)
combo.place(x=260, y=400)
Btn=Button(ventana, text="Send",)
Btn.place(x=200,y=500)
txt=scrolledtext.ScrolledText(ventana, width=40, height=10)
txt.place(x=260, y= 550)
txt.insert(INSERT, Nombre)
You can use Button(..., command=function_name) to execute function_name() when you press button. This function can get selected value and insert in scrolledtext.
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.scrolledtext as scrolledtext
def send_data():
txt.insert('end', combo.get() + '\n')
root = tk.Tk()
label = tk.Label(root, text="Location:")
label.pack()
combo = ttk.Combobox(root, values=("Chicago","NY", "Texas"))
combo.pack()
combo.current(0)
button = tk.Button(root, text="Send", command=send_data)
button.pack()
txt = scrolledtext.ScrolledText(root)
txt.pack()
root.mainloop()