Is it possible to resize text in a tkinter box in python? I know I can add text and, when doing so, among other traits, define the font size. Is it possible to resize the text if say, something triggers the program to do so?
If you are using a Text box you can use tags for formatting its contents. For instance:
#!/usr/bin/env python
import Tkinter
import tkFont
class App:
def __init__(self):
# Set up the text box
self.root = Tkinter.Tk()
self.text = Tkinter.Text(self.root, width=20, height=5)
self.text.insert(Tkinter.INSERT, "Hello...")
self.text.pack()
# set up a mouse event
self.text.bind("<Button-1>", self.click)
# Set Tkniter's main loop
self.root.mainloop()
def click(self, event):
self.text.tag_add("here", "1.0", "1.4")
self.text.tag_config("here", background="yellow", font=tkFont.Font(size=36))
if __name__ == "__main__":
App()
will change the background and size of a section of the entered text when you click the box with your mouse.
Related
So I am working on making an application inside of Tkinter and I am not that experienced in it, but I noticed that when you press a button it moves the text inside down and to the right just a couple pixels and I can't figure out how to make it not do that. Any help finding what parameter I need to change would be greatly appreciated.
I tried finding any docs on the subject but whenever I looked it up I would only get things that pertained to how to place a button on the window or in a frame/canvas with place, grid, pack.
will you can change the relief value to be SUNKEN and adjust the border values like this
Button(root, text = 'Click me !', borderwidth=5, relief=SUNKEN)
the button will look a little weird
or you can use widget other than a button just like this answer (https://stackoverflow.com/a/10674950/20411925)
import tkinter
class main:
def __init__(self,root):
# make a label with some space around the text
self.lbl1 = tkinter.Label(root,
width = 16, height = 4,
text = "Foobar")
self.lbl1.pack()
# Call a function when lbl1 is clicked
# <Button-1> means a left mouse button click
self.lbl1.bind("<Button-1>", self.yadda)
self.lbl1.bind("<Enter>", self.green)
self.lbl1.bind("<Leave>", self.red)
def yadda(self, event):
self.lbl1.config(text="Clicked!")
def green(self, event):
self.lbl1.config(bg="green")
def red(self,event):
self.lbl1.config(bg="red")
if __name__ == "__main__":
root = tkinter.Tk()
main(root)
root.mainloop()
How can I make a frame in Tkinter display in fullscreen mode? I saw this code, and it's very useful…:
>>> import Tkinter
>>> root = Tkinter.Tk()
>>> root.overrideredirect(True)
>>> root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
…but is it possible to edit the code so that hitting Esc automatically makes the window "Restore down"?
I think this is what you're looking for:
Tk.attributes("-fullscreen", True) # substitute `Tk` for whatever your `Tk()` object is called
You can use wm_attributes instead of attributes, too.
Then just bind the escape key and add this to the handler:
Tk.attributes("-fullscreen", False)
An answer to another question alluded to this (with wm_attributes). So, that's how I found out. But, no one just directly went out and said it was the answer for some reason. So, I figured it was worth posting.
Here's a working example (tested on Xubuntu 14.04) that uses F11 to toggle fullscreen on and off and where escape will turn it off only:
import sys
if sys.version_info[0] == 2: # Just checking your Python version to import Tkinter properly.
from Tkinter import *
else:
from tkinter import *
class Fullscreen_Window:
def __init__(self):
self.tk = Tk()
self.tk.attributes('-zoomed', True) # This just maximizes it so we can see the window. It's nothing to do with fullscreen.
self.frame = Frame(self.tk)
self.frame.pack()
self.state = False
self.tk.bind("<F11>", self.toggle_fullscreen)
self.tk.bind("<Escape>", self.end_fullscreen)
def toggle_fullscreen(self, event=None):
self.state = not self.state # Just toggling the boolean
self.tk.attributes("-fullscreen", self.state)
return "break"
def end_fullscreen(self, event=None):
self.state = False
self.tk.attributes("-fullscreen", False)
return "break"
if __name__ == '__main__':
w = Fullscreen_Window()
w.tk.mainloop()
If you want to hide a menu, too, there are only two ways I've found to do that. One is to destroy it. The other is to make a blank menu to switch between.
self.tk.config(menu=self.blank_menu) # self.blank_menu is a Menu object
Then switch it back to your menu when you want it to show up again.
self.tk.config(menu=self.menu) # self.menu is your menu.
This creates a fullscreen window. Pressing Escape resizes the window to '200x200+0+0' by default. If you move or resize the window, Escape toggles between the current geometry and the previous geometry.
import Tkinter as tk
class FullScreenApp(object):
def __init__(self, master, **kwargs):
self.master=master
pad=3
self._geom='200x200+0+0'
master.geometry("{0}x{1}+0+0".format(
master.winfo_screenwidth()-pad, master.winfo_screenheight()-pad))
master.bind('<Escape>',self.toggle_geom)
def toggle_geom(self,event):
geom=self.master.winfo_geometry()
print(geom,self._geom)
self.master.geometry(self._geom)
self._geom=geom
root=tk.Tk()
app=FullScreenApp(root)
root.mainloop()
I think if you are looking for fullscreen only, no need to set geometry or maxsize etc.
You just need to do this:
-If you are working on ubuntu:
root=tk.Tk()
root.attributes('-zoomed', True)
-and if you are working on windows:
root.state('zoomed')
Now for toggling between fullscreen, for minimising it to taskbar you can use:
Root.iconify()
Here's a simple solution with lambdas:
root = Tk()
root.attributes("-fullscreen", True)
root.bind("<F11>", lambda event: root.attributes("-fullscreen",
not root.attributes("-fullscreen")))
root.bind("<Escape>", lambda event: root.attributes("-fullscreen", False))
root.mainloop()
This will make the screen exit fullscreen when escape is pressed, and toggle fullscreen when F11 is pressed.
This will create a completely fullscreen window on mac (with no visible menubar) without messing up keybindings
import tkinter as tk
root = tk.Tk()
root.overrideredirect(True)
root.overrideredirect(False)
root.attributes('-fullscreen',True)
root.mainloop()
Yeah mate i was trying to do the same in windows, and what helped me was a bit of lambdas with the root.state() method.
root = Tk()
root.bind('<Escape>', lambda event: root.state('normal'))
root.bind('<F11>', lambda event: root.state('zoomed'))
Just use:
# importing tkinter for gui
import tkinter as tk
# creating window
window = tk.Tk()
# setting attribute
window.state('zoomed')
window.title("Full window")
# creating text label to display on window screen
label = tk.Label(window, text="Hello world!")
label.pack()
window.mainloop()
If you want to hide everything except the window, you can also use:
import tkinter as tk
root = tk.Tk()
root.overrideredirect(True)
root.overrideredirect(False)
root.attributes('-fullscreen',True)
root.title("FullScreen")
label = tk.Label(root, text="Hello! Press 🪟 logo on the keypad > select the python logo > Close window to close")
labela = tk.Label(root, text="🎉🎉🎉")
label.pack()
labela.pack()
root.mainloop()
root = Tk()
root.geomentry('1599x1499')
I would like to make a Tkinter window able to ask for a multi line entry
(so the user will add one or more lines of text)
And then when we clic on the button be able to retrieve the values entered by user for further use.
Until now I have this script:
from Tkinter import *
import ScrolledText
class EntryDemo:
def __init__(self, rootWin):
#Create a entry and button to put in the root window
self.textfield = ScrolledText(rootWin)
#Add some text:
self.textfield.delete(0,END)
self.textfield.insert(0, "Change this text!")
self.textfield.pack()
self.button = Button(rootWin, text="Click Me!", command=self.clicked)
self.button.pack()
def clicked(self):
print("Button was clicked!")
eText = self.textfield.get()
print("The Entry has the following text in it:", eText)
#Create the main root window, instantiate the object, and run the main loop
rootWin = Tk()
#app = EntryDemo( rootWin )
rootWin.mainloop()
But it didn't seem to work, A window appear with nothing inside.
Could you help me?
#########EDIT
New code:
from Tkinter import *
import ScrolledText
class EntryDemo:
def __init__(self, rootWin):
self.textfield = ScrolledText.ScrolledText(rootWin)
#Add some text:
#self.textfield.delete(0,END)
self.textfield.insert(INSERT, "Change this text!")
self.textfield.pack()
self.button = Button(rootWin, text="Click Me!", command=self.clicked)
self.button.pack()
def clicked(self):
eText = self.textfield.get(1.0, END)
print(eText)
rootWin = Tk()
app = EntryDemo( rootWin )
rootWin.mainloop()
Sorry if it look like done with no effort by some down voters (even if I spent more than a day on it) but the Multi line text entry is not exactly what we can call well documented to learn by ourself.
Your first problem is that you commented out the app = EntryDemo( rootWin ) call, so you're not actually doing anything but creating a Tk() root window, then starting its main loop.
If you fix that, your next problem is that you're trying to use the ScrolledText module as if it were a class. You need the ScrolledText.ScrolledText class.
If you fix that, your next problem is that you're trying to delete from an empty text field, which is going to raise some kind of Tcl index error, and then you're also trying to insert at position 0 in an empty text field, which will raise the same error. There's no reason to do the delete at all, and for the insert you probably want to use INSERT as the position.
You still have multiple problems after that, but fixing these three will get your edit box up and displayed so you can start debugging everything else.
A working example based on your code. Note the comment above that both the file you import and the class within the file are named "ScrolledText"
from Tkinter import *
import ScrolledText
class EntryDemo:
def __init__(self, rootWin):
#Create a entry and button to put in the root window
self.textfield = ScrolledText.ScrolledText(rootWin)
self.textfield.pack()
#Add some text:
self.textfield.delete('1.0', END)
self.textfield.insert('insert', "Change this text!")
self.button = Button(rootWin, text="Click Me!",
command=self.clicked)
self.button.pack()
def clicked(self):
print("Button was clicked!")
eText = self.textfield.get('1.0', END)
print("The Entry has the following text in it:", eText)
self.textfield.delete('1.0', END)
rootWin = Tk()
app = EntryDemo( rootWin )
rootWin.mainloop()
Background
Each one of the items appearing in my text widget represent tasks. I am using a Text widget over a ListBox because Text widget allows different colors for different items. The color of each task is its status. Is it possible to set a separate LC command for each task?
I have a split-pane application with tasks on the right (possibly scrollable), and I want clicking on a task to open it up for scrutiny in the pane on the left.
Main Question
Can I in any way activate separate events on left-clicking separate lines in a Python Text Tkinter widget?
Just set a binding on <1>. It's easy to get the line number that was clicked on using the index method of the widget and the x/y coordinate of the event.
Here's a simple example:
import Tkinter as tk
class ExampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.status = tk.Label(self, anchor="w")
self.status.pack(side="bottom", fill="x")
self.text = tk.Text(self, wrap="word", width=40, height=8)
self.text.pack(fill="both", expand=True)
self.text.bind("<1>", self.on_text_button)
for n in range(1,20):
self.text.insert("end", "this is line %s\n" % n)
def on_text_button(self, event):
index = self.text.index("#%s,%s" % (event.x, event.y))
line, char = index.split(".")
self.status.configure(text="you clicked line %s" % line)
if __name__ == "__main__":
app = ExampleApp()
app.mainloop()
I think you can do something like this. tkHyperlinkManger does it ( http://effbot.org/zone/tkinter-text-hyperlink.htm )
Since you're already coloring the lines differently, I assume you're using tag_config. Then all you need is tag_bind to bind a callback to the region of text.
I am working with buttons in Tkinter, Python.
The thing is when I click in one button the text of the button shakes. It might be a default behavior for this widget and I don't know how to disable it and make it static.
I assume that you mean the relief change from raised to sunken when you click a button.
This is what I found on http://wiki.tcl.tk/1048 (click 'Show Discussion' to see it):
Unfortunately, the relief used when you click is hardcoded (as
'sunken'), so you can't configure it per-widget without hacking the Tk
internals for the binding for buttons.
So the simplest way around this would be to always make the button appear sunken
MyButton = Tkinter.Button(
self.frame,
text = "Foobar",
command = self.foobar,
relief=Tkinter.SUNKEN
)
The disadvantage of that is that it might make the button look unresponsive.
You can also use a widget other than a button to be used as a clickable item (suggested by Joel Cornett). Here is a simple example with a label used as a button:
import Tkinter
class main:
def __init__(self,root):
# make a label with some space around the text
self.lbl1 = Tkinter.Label(root,
width = 16, height = 4,
text = "Foobar")
self.lbl1.pack()
# Call a function when lbl1 is clicked
# <Button-1> means a left mouse button click
self.lbl1.bind("<Button-1>", self.yadda)
self.lbl1.bind("<Enter>", self.green)
self.lbl1.bind("<Leave>", self.red)
def yadda(self, event):
self.lbl1.config(text="Clicked!")
def green(self, event):
self.lbl1.config(bg="green")
def red(self,event):
self.lbl1.config(bg="red")
if __name__ == "__main__":
root = Tkinter.Tk()
main(root)
root.mainloop()