I am trying to create a GUI form using Python tkinter. For a ttk Entry widget, I want to set a default text. I do that using the insert() as shown below-
from tkinter import *
from tkinter import ttk
root = Tk()
e = ttk.Entry(root, width=20)
e.pack()
e.insert(0, 'text greater than width of the widget')
The widget shows the beginning portion of the inserted text. Is there a way that I can make it scroll to the end of the inserted text by default?
You can call the xview method to scroll a given index into view:
e.xview("end")
Related
I am writing a program in python in which I want the Text widget in tkinter to have a scrollbar .
this is a part of my code -
import tkinter as tk
import tkinter.font as tkfont
window = tk.Tk()
display_text = "START\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nEND"
font_1 = tkfont.Font(family='Helvetica', size=22, weight='bold')
my_label_1 = tk.Text(window, wrap=tk.WORD, bd=0, fg="white", bg="black", font=font_1, width=76, height=21,)
_scrollbar = tk.Scrollbar(window, command=my_label_1.yview)
my_label_1.configure(yscrollcommand=_scrollbar.set)
my_label_1.tag_configure("center", justify='center')
my_label_1.insert(1.0, display_text)
_scrollbar.pack(side=tk.RIGHT, ipadx=3, ipady=500, pady=(0, 48))
my_label_1.pack()
window.mainloop()
But I have 500 strings which I import from a csv file, when I put the strings in this code the strings go in disply_text variable, this works fine.
But for some of those strings the text widget is big enough and the scrollbar is not needed as the text widget is not scrollable for those. but the scrollbar still shows up it is disabled though but I don't want it to be there.
I know pack_forget() or grid_forget() can do this job, But Then what parameter should I put so I know that this iteration of text is not scrollable on the Text widget.
And my window is of fixed size i.e. window.resizable(False, False).
So I am actually writing a simple GUI program which makes use of ScrolledText widget from tkinter.scrolledtext module.
The problem is this ScrolledText widget seems to take up the complete space available in the parent window. It disallows me from putting in any other widget in the same parent window. I have tried using both grid and pack GeoManagers (i know place isn't very useful in all cases), but the other widgets won't show up (neither above the scrolledtext widget nor below it).
HERE IS THE CODE--
import tkinter as tk
import tkinter.scrolledtext as sct
win2 = tk.Tk()
win2.geometry('1150x680')
win2.wm_geometry('+80+20')
txtbox = sct.ScrolledText(win2, width=500, height=350, bg='#fff', fg='#00f')
txtbox.grid(row=0, column=0)
txt = '<ABOUT 60 Lines TEXT HERE>'
txtbox.insert(1.0, txt)
txtbox.configure(state=tk.DISABLED)
tk.Button(win2, text='Got It', command=win2.destroy).grid(row=1, column=0)
This code is actually a part of a static method (i don't think makes a difference). When this is run the only thing visible on the screen is the scrolledtext widget with those 60 lines (i have tried it with 2 lines as well - still doesn't work).
The same happens when using pack().
To my surprise the only thing i could find in documentation is this::
ScrolledText Documentation
I don't know what I am missing here so please suggest me a way around this.
Thanks You :)
Solution with grid
The problem is the configuration of the grid: by default, the grid cells expand to fit the content. In your case the text widget is so big that the button in the row below is out of the screen. To fix that, you need to configure the first row and column to stretch with the GUI:
win2.rowconfigure(0, weight=1)
win2.columnconfigure(0, weight=1)
and make the text widget fill the cell, using the sticky option:
txtbox.grid(row=0, column=0, sticky='ewns')
This way the text widget will adapt to the window size and not the other way around.
Full code:
import tkinter as tk
import tkinter.scrolledtext as sct
win2 = tk.Tk()
win2.geometry('1150x680')
win2.wm_geometry('+80+20')
win2.rowconfigure(0, weight=1)
win2.columnconfigure(0, weight=1)
txtbox = sct.ScrolledText(win2, width=500, height=350, bg='#fff', fg='#00f')
txtbox.grid(row=0, column=0, sticky='ewns')
txt = '<ABOUT 60 Lines TEXT HERE>'
txtbox.insert(1.0, txt)
txtbox.configure(state=tk.DISABLED)
tk.Button(win2, text='Got It', command=win2.destroy).grid(row=1, column=0)
Alternative method, using pack
You can use pack with the options fill='both' and expand=True to achieve the same result as with grid. In this case, the additional trick is to pack the button first to ensure that it has enough space to show in the window. Code:
import tkinter as tk
import tkinter.scrolledtext as sct
win2 = tk.Tk()
win2.geometry('1150x680')
win2.wm_geometry('+80+20')
tk.Button(win2, text='Got It', command=win2.destroy).pack(side='bottom')
txtbox = sct.ScrolledText(win2, width=500, height=350, bg='#fff', fg='#00f')
txtbox.pack(fill='both', expand=True)
txt = '<ABOUT 60 Lines TEXT HERE>'
txtbox.insert(1.0, txt)
txtbox.configure(state=tk.DISABLED)
I'm trying to create a static Toplevel window with some text and no title bar with Tkinter in Python 3.6.1 on OS X 10.12.5. I believe that wm_overrideredirect(True) is the method I need to achieve this, but whenever I enter this line, the Toplevel window is not displayed. When I remove this line, the Toplevel is displayed (with a title bar).
What am I doing wrong!? This is the code I have.
import tkinter as tk
root = tk.Tk()
tw = tk.Toplevel()
tw.wm_overrideredirect(True) # Toplevel will not display due to this line
tk.Label(tw, text='Some text').pack()
root.mainloop()
Hi I am trying to use the ttk Combobox to create a dropdown with options .
While doing so i can configure the font size of the default value passed to it .
But when i click the arrow the font size of the other values remains the same .I am developing the app for touchscreen , so i need to provide proper size .
Heres the sample code , when i run the code the size of A is bigger , button the on clicking the arrow key i see the other values are of default size .
#! /usr/bin/python
from Tkinter import *
import ttk
class Application:
def __init__(self, parent):
self.parent = parent
self.combo()
def combo(self):
self.box_value = StringVar()
self.box = ttk.Combobox(self.parent, textvariable=self.box_value,font=("Helvetica",20))
self.box['values'] = ('A', 'B', 'C')
self.box.current(0)
self.box.grid(column=0, row=0)
if __name__ == '__main__':
root = Tk()
app = Application(root)
root.mainloop()
The thing is that the dropdown menu of the ttk Combobox is actually a simple Tkinter Listbox so it isn't affected by the ttk style. If it would be possible to get a reference to the Listbox from the Combobox, changing the font would be easy. However, I couldn't find a way to do so in Tkinter.
Edited as per patthoyts' very useful comment.
What you can do is change the font for all Listboxes that are part of a Combobox using
bigfont = tkFont.Font(family="Helvetica",size=20)
root.option_add("*TCombobox*Listbox*Font", bigfont)
That changes the font of all Listbox widgets that are part of a ttk Combobox and that are created after calling this.
This does affect all new Comboboxes, but I assume that's what you want. If you want the new font only for this Combobox, you could choose to create this Combobox as the last widget and call self.parent.option_add("*TCombobox*Listbox*Font", bigfont) right before creating this Combobox. Then only the Listbox under this Combobox will have the new font.
If you want all widgets to have the bigger font, you can use
root.option_add("*Font", bigfont)
or you can change the default font as described in this answer.
While working on the same issue as the OP, the problem of the arrow size mentioned in the comments of the accepted answer by Deepworks and fhdrsdg came up. Unfortunately I'm new and can't comment, hence I'm posting this as an answer. There is actually a way to set the arrow size via the Style "arrowsize" option.
style = ttk.Style()
style.configure('W.TCombobox',arrowsize = 60)
cBox = ttk.Combobox(self, style='W.TCombobox')
This allows you to increase the arrow size to match the font size of the rest of the widget.
I found the reference to the "arrowsize" option here:
Tcl8.6.10/Tk8.6.10 Documentation > Tk Commands > ttk_combobox
from Tkinter import *
root = Tk()
root.title("Whois Tool")
text = Text()
text1 = Text()
text1.config(width=15, height=1)
text1.pack()
def button1():
text.insert(END, text1)
b = Button(root, text="Enter", width=10, height=2, command=button1)
b.pack()
scrollbar = Scrollbar(root)
scrollbar.pack(side=RIGHT, fill=Y)
text.config(width=60, height=15)
text.pack(side=LEFT, fill=Y)
scrollbar.config(command=text.yview)
text.config(yscrollcommand=scrollbar.set)
root.mainloop()
How can I add the data from a text widget to another text widget?
For example, I'm trying to insert the data in text1 to text, but it is not working.
You are trying to insert a Text reference at the end of another Text widget (does not make much sense), but what you actually want to do is to copy the contents of a Text widget to another:
def button1():
text.insert(INSERT, text1.get("1.0", "end-1c"))
Not an intuitive way to do it in my opinion. "1.0" means line 1, column 0. Yes, the lines are 1-indexed and the columns are 0-indexed.
Note that you may not want to import the entire Tkinter package, using from Tkinter import *. It will likely lead to confusion down the road. I would recommend using:
import Tkinter
text = Tkinter.Text()
Another option is:
import Tkinter as tk
text = tk.Text()
You can choose a short name (like "tk") of your choice. Regardless, you should stick to one import mechanism for the library.