How do i resize this labelframe in tkinter? - python

So I tried to make this labelframe wider by using the basic width and width option.
Here's my given minimal code.
from tkinter import *
from tkinter import ttk
app = Tk()
app.resizable(False, False)
mainLayout = ttk.Frame(app, padding=10)
mainLayout.grid()
settings = ttk.Labelframe(mainLayout, text="Settings", padding=10, width=1000)
settings.grid()
ttk.Label(settings, text="Length limit (in seconds)").grid()
ttk.Spinbox(settings, from_=60, to=600, width=4).grid()
app.mainloop()
minimalized preview:
used in application:
i want to get this labelframe little bit bigger and make the inside centered, But i had no knowledge to do so, Any help will apreciated!

It seems like you just want to have a main_frame in the app. For simplicity I've used .pack with the options fill and expand with the constants tkinter.BOTH to stretch the widget in both (x,y) direction and True to consume extra space. (This is one of the reasons why wildcard imports are discouraged, you can be unaware of overwriting something, use import tkinter as tk instead). Same happens with the LabelFrame, you may could delete one of the containers, but that is up to you.
In LabelFrame I have configured the grid and gave the instruction that the column 0 should get the extra space with the priority/weight 1.
In addition, I gave your Spinbox a little bit more width, changed the size of the window and separated the constructor from the geometrymethod.
To get in touch with the geometry management in tkinter, you could play around with the instructions (e.g. comment some out) and see what happens.
from tkinter import *
from tkinter import ttk
app = Tk()
app.geometry('500x500')
app.resizable(False, False)
mainLayout = ttk.Frame(app, padding=10)
mainLayout.pack(fill=BOTH,expand=True)
settings = ttk.Labelframe(mainLayout, text="Settings", padding=10, width=1000)
settings.pack(fill=BOTH,expand=True)
settings.columnconfigure(0,weight=1)
my_label = ttk.Label(settings, text="Length limit (in seconds)")
my_label.grid()
my_spinbox = ttk.Spinbox(settings, from_=60, to=600, width=20)
my_spinbox.grid()
app.mainloop()

Related

Prevent scrolledtext from taking up entire parent window disallowing other widgets from showing up

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)

How to remove indent left on the widget Tkinter Treeview?

Can I somehow remove this indentation? I know that I can remove the first column, but then I can not insert the image.
import tkinter as tk
import tkinter.ttk as ttk
from PIL import Image, ImageTk
class App:
def __init__(self, master):
self.tree = ttk.Treeview(master)
self.tree.heading('#0', text='Directory', anchor='w')
self.tree["columns"]=("num_files")
self.tree.heading('num_files', text='Number of files', anchor='w')
self.image_tk = ImageTk.PhotoImage(Image.open('icon.png'))
for i in range(10):
self.tree.insert('', 'end', text="Dir %d" % (i+1), values=(15), open=False, image=self.image_tk)
self.tree.pack(expand=1,fill="both")
root = tk.Tk()
app = App(root)
root.mainloop()
Assuming the indentation you're referring to is the left side of the first column (left of all the icons), you can adjust the entire widget padding as needed. For your application, start with:
self.tree = ttk.Treeview(master, padding=[-15,0,0,0])
What you have highlighted in red is the area in which the Treeview's indicator resides -- the open or close toggle icon -- that is shown if any of the Item objects contain subitems of their own.
One way to accomplish removing that area would be to just remove the indicator altogether while using a ttk theme that allows you to do so.
s = ttk.Style()
s.theme_use('default')
s.configure('Treeview.Item', indicatorsize=0)
This approach seems to only work for the default, clam and classic themes on Windows 10. For other themes, Ron's answer may be your best bet.

Window size of tkinter program

Actually, I thought that the tkinter window is as large as it needs to be. Unfortunately, it's not true for me (I use Lubuntu 16.04).
My question:
Can please someone explain me the examples below? I don't understand tkinter's behaviour.
Here an example:
import tkinter as tk
root = tk.Tk()
menubar = tk.Menu(root)
file_menu = tk.Menu(menubar)
menubar.add_cascade(label="This is a very long string because it's just a test . . . ", menu=file_menu)
file_menu.add_command(label="New", accelerator="Ctrl+N")
root.config(menu=menubar)
root.mainloop()
Screenshot:
Window is not large enough. Why?
Another example (with container Frame):
import tkinter as tk
root = tk.Tk()
container = tk.Frame(root).pack()
menubar = tk.Menu(container)
file_menu = tk.Menu(menubar)
menubar.add_cascade(label="This is a very long string because it's just a test . . . ", menu=file_menu)
file_menu.add_command(label="New", accelerator="Ctrl+N")
root.config(menu=menubar)
root.mainloop()
Screenshot: very, very small! You can't really see it. But why is so?
The windows size in Tk is a function of the geometry manager for the window. In the case of your toplevel window the geometry manager is 'wm' which means it is managed by the platform window manager. This asks the application window for a size or sets a default. The menu widget does not provide a size so doesn't affect the geometry calculations. So you get a default toplevel size as you have no widgets managed inside.
In the second case you created a frame with no size dimensions which defaults to a height and width of 0. You pack this which sets the toplevel wdget to use the pack geometry manager for its slaves. This asks the slaves (your frame) how much space they require and sets the master (the toplevel) to a suitable size to contain them. Here that is 0. If you set the width and height of the frame the toplevel will expand to contain that.

Making themed TK button have a border

With the default theme on Linux, I can't get the button to have a border. Here's my attempt:
from tkinter import *
from tkinter.ttk import *
tk = Tk()
style = Style()
style.configure('TButton.border', background='red')
button = Button(tk, text='Hello')
button.pack()
mainloop()
I have found theming very difficult because it is not clear what I can change. Sometimes changes seem to do nothing.
It depends on what theme you're using; not all themes associate all widgets with all the elements they might have, and some (e.g., the OSX native theme, aqua) really very tightly control what the look and feel of many elements are (since the theme engine in that case delegates to the native drawing code). It's entirely possible that the theme you're using just doesn't permit red borders at all.
Try switching to a different themeā€¦
ttk.Style().theme_use('clam')
Simple proposal ... if you really want a border. Understand it should not be satisfactory.
import Tkinter as tk
root = tk.Tk()
def nothing(event=None):
print "click"
bgbutton= tk.Button(root, )
bgbutton.pack()
bgbutton.configure(relief=tk.GROOVE, borderwidth=5, background="#2244FF", activebackground="#FF0000", highlightcolor="#00FF00")
button= tk.Button(bgbutton, text="Glance at my border", command=nothing)
button.pack()
button.configure(relief=tk.GROOVE, borderwidth=2)
root.mainloop()

Combobox fontsize in tkinter

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

Categories

Resources