Entry widgets seem only to deal with single line text. I need a multiline entry field to type in email messages.
Anyone has any idea how to do that?
You could use the Text widget:
from tkinter import *
root = Tk()
text = Text(root)
text.pack()
root.mainloop()
Or with scrolling bars using ScrolledText:
from tkinter import *
from tkinter.scrolledtext import ScrolledText
root = Tk()
ScrolledText(root).pack()
root.mainloop()
Just use Text() widget.
For example:
import tkinter as tk
root = tk.Tk()
text = tk.Text(root)
text.pack()
root.mainloop()
Output:
Related
I am trying to make a bind_class to a ttk Entry widget but it does not work. It only work properly when I bind each Entry widget separately.
Would you help me?
Thank you in advance
from tkinter import ttk
from tkinter.ttk import*
from tkinter import *
root = Tk()
def clicker(event):
myLabel = ttk.Label( root, text = 'You clicked a button' )
myLabel.pack()
myEntry = ttk.Entry( root )
myEntry.pack()
root.bind_class( 'Entry', '<Button-1>', clicker )
root.mainloop()
When I replaced 'Entry' by 'TEntry' it worked perfectly
I'm a Python beginner that trying to learn my way through Tkinter, and I need your help.
Let say I create a simple button like this:
import tkinter as tk
window = tk.Tk()
button = tk.Button(text="Hello World!")
button.pack()
window.mainloop()
Is there a way that I can hide and then display the text again? Given that I can create two more buttons that will do the job of hiding and displaying. I have tried to use button.pack_forget(), but it will hide the entire button instead.
Any help would be appreciated.
To make it look like the buttons text vanishes,you can make the text color as same as the background color via cget method.
import tkinter as tk
def hide_text():
color = button['bg']
button.config(foreground=color, activeforeground=color)
window = tk.Tk()
button = tk.Button(text="Hello World!",command=hide_text)
button.pack()
window.mainloop()
Approach 2 ttk.Button:
import tkinter as tk
from tkinter import ttk
def hide_text():
button.config(text='')
window = tk.Tk()
button = ttk.Button(text="Hello World!",width=100,command=hide_text)
button.pack()
window.mainloop()
Approach3 using style:
import tkinter as tk
from tkinter import ttk
def change_button_style(event):
widget = event.widget
if widget['style'] == 'TButton':
widget.configure(style='VanishedText.TButton')
else:
event.widget.config(style='TButton')
BACKGROUND = '#f0f0f0'
FOREGROUND = '#000000'
window = tk.Tk()
window.configure(bg=BACKGROUND)
style = ttk.Style()
style.theme_use('default')
button = ttk.Button(text="Hello World!",style='VanishedText.TButton')
button.bind('<ButtonPress-1>',change_button_style)#,command=change_button_style
button.pack()
style.map('VanishedText.TButton',
foreground =[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)],
background =[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)],
focuscolor=[('disabled',BACKGROUND),
('!disabled',BACKGROUND),
('pressed',BACKGROUND),
('!pressed',BACKGROUND),
('active',BACKGROUND),
('!active',BACKGROUND)])
window.mainloop()
You Can Simply Config And Set Text :
import tkinter as tk
from tkinter import ttk
win=tk.Tk()
def hidetext():
button.config(text="")
button=ttk.Button(win,text="FooBar",command=hidetext)
button.pack()
win.mainloop()
I'm using the simple message-boxes provided in tkinter and I'm wondering if there's any way to change the font.
This is the general idea of what I want, but the font= option doesn't work.
from tkinter import Tk
import tkinter.messagebox as tkmsg
_root = Tk()
_root.withdraw()
tkmsg.showinfo(
"Info",
"Some monospaced text",
font=("Monospace", 15)
)
_root.destroy()
Is there any way to change the font or do I have to use a custom dialog?
See here for how to change the dialog box text: Control Font in tkMessageBox
In short (copied verbatim from the link above):
You can configure the font for just dialog boxes by doing the following:
from Tkinter import *
import tkMessageBox
r = Tk()
r.option_add('*Dialog.msg.font', 'Helvetica 12')
tkMessageBox.showinfo(message='Hello')
Be sure to call r.option_clear() to set the font back to normal afterwards.
You can change the default font for captions:
import tkinter as tk
from tkinter import messagebox as mb
from tkinter import font
root = tk.Tk()
font1 = font.Font(name='TkCaptionFont', exists=True)
font1.config(family='courier new', size=20)
mb.showinfo(message='Hello')
You should write your own messagbox. Tkinter invoke system dialog for Windows or Mac and genetate dialogs for Linux. In all cases is imposible change Tkinter dialogs.
you can't.
write your own messagebox using the toplevel widgted (tkinter.Toplevel()) and label!
Something like this (from http://effbot.org/tkinterbook/label.htm)
from Tkinter import *
master = Tk()
w = Label(master, text="Hello, world!")
w.pack()
mainloop()
i hope it helps!
Edit: This is a very old answer, 3 years later someone said it's possible:
Control Font in tkMessageBox
Something like this:
from Tkinter import *
root = Tk()
but = Button(root, text = "button")
but.pack()
#When I try:
but.destroy()
but.pack()
I get an error:
TclError: bad window path name ".37111768"
The pack_forget method will hide the widget and you can pack or grid it again later.
http://effbot.org/tkinterbook/pack.htm
I have managed to get it working :) here is my work:
from Tkinter import *
def changebutton():
but.destroy()
secondbut=Button(root,text="changed")
secondbut.pack()
if __name__=='__main__':
root=Tk()
global but
but= Button(root,text="button",command=changebutton)
but.pack()
root.mainloop()
I'd like to be able to open the App GUI and have it automatically place the cursor into a particular text widget. Best case scenario is: as soon as the app is launched someone can start typing without having to click on the text widget. This is just a small example displaying the issue:
from Tkinter import *
root = Tk()
Window = Frame(root)
TextWidget = Text(Window)
TextWidget.pack()
Window.pack()
root.mainloop()
You use the focus_set method. For example:
from Tkinter import *
root = Tk()
Window = Frame(root)
TextWidget = Text(Window)
TextWidget.pack()
Window.pack()
TextWidget.focus_set()
root.mainloop()