Remove automatic gap between gridded python Tkinter widgets - python

I'm using python2 on Windows. When I run the followig code, I get a gap between the two canvas (see picture below), although there is no padding specified when I grid them.
Is there any possibility to remove this?
import Tkinter as tk
import ttk
class App(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.c1 = tk.Canvas(master=self, background='white', borderwidth=0,
relief=tk.FLAT)
self.c2 = tk.Canvas(master=self, background='white', borderwidth=0,
relief=tk.FLAT)
self.c1.grid(row=0, column=0, sticky=tk.NSEW)
self.c2.grid(row=1, column=0, sticky=tk.NSEW)
self.mainloop()
App()
Thanks for help!

You need to set highlightthickness to zero as well.
self.c1 = tk.Canvas(..., highlightthickness=0)
From the canvas page of effbot highlightthickness explained as:
The width of the highlight border. The default is system specific
(usually one or two pixels). (highlightThickness/HighlightThickness)

Related

Classes and Styling in Tkinter

I'm still in my first week of Python, so I'm sorry if this is an obvious mistake...
I want to style a Frame in Tkinter with rounded corners, therefore I got a base64 encoded image, which I applied using ttk.Style following this answer (Tkinter: How to make a rounded corner text widget?) and that worked fine.
My problem is now adapting this into a Class structured program, there the frame is not showing at all.
I replicated the Problem in the code below.
The first window is showing the problem, and the second one how its supposed to look.
How do I make this work? And could there be any new conflicts coming up when I'm not packing the frame to the main window, but into another Frame?
Thanks in advance!
import tkinter as tk
from tkinter import ttk
borderImageData = '''
R0lGODlhQABAAPcAAHx+fMTCxKSipOTi5JSSlNTS1LSytPTy9IyKjMzKzKyq
rOzq7JyanNza3Ly6vPz6/ISChMTGxKSmpOTm5JSWlNTW1LS2tPT29IyOjMzO
zKyurOzu7JyenNze3Ly+vPz+/OkAKOUA5IEAEnwAAACuQACUAAFBAAB+AFYd
QAC0AABBAAB+AIjMAuEEABINAAAAAHMgAQAAAAAAAAAAAKjSxOIEJBIIpQAA
sRgBMO4AAJAAAHwCAHAAAAUAAJEAAHwAAP+eEP8CZ/8Aif8AAG0BDAUAAJEA
AHwAAIXYAOfxAIESAHwAAABAMQAbMBZGMAAA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+8Dwo9nL9I8evjWsdOX6D59
fPH71Xeef/kFyB93/sln4EP2Ebjegg31B5+CEDLUIH4PVqiQhOABqKFCF6qn
34cHcfjffCQaFOJtGaZYkIkUuljQigXK+CKCE3po40A0trgjjDru+EGPI/6I
Y4co7kikkAMBmaSNSzL5gZNSDjkghkXaaGIBHjwpY4gThJeljFt2WSWYMQpZ
5pguUnClehS4tuMEDARQgH8FBMBBBExGwIGdAxywXAUBKHCZkAIoEEAFp33W
QGl47ZgBAwZEwKigE1SQgAUCUDCXiwtQIIAFCTQwgaCrZeCABAzIleIGHDD/
oIAHGUznmXABGMABT4xpmBYBHGgAKGq1ZbppThgAG8EEAW61KwYMSOBAApdy
pNp/BkhAAQLcEqCTt+ACJW645I5rLrgEeOsTBtwiQIEElRZg61sTNBBethSw
CwEA/Pbr778ABywwABBAgAAG7xpAq6mGUUTdAPZ6YIACsRKAAbvtZqzxxhxn
jDG3ybbKFHf36ZVYpuE5oIGhHMTqcqswvyxzzDS/HDMHEiiggQMLDxCZXh8k
BnEBCQTggAUGGKCB0ktr0PTTTEfttNRQT22ABR4EkEABDXgnGUEn31ZABglE
EEAAWaeN9tpqt832221HEEECW6M3wc+Hga3SBgtMODBABw00UEEBgxdO+OGG
J4744oZzXUEDHQxwN7F5G7QRdXxPoPkAnHfu+eeghw665n1vIKhJBQUEADs=
'''
class Application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
style = ttk.Style()
style.theme_use('clam')
borderImage = tk.PhotoImage("borderImage", data=borderImageData)
style.element_create("RoundedFrame", "image", borderImage, border=16, sticky="nsew")
style.layout("RoundedFrame", [("RoundedFrame", {"sticky": "nsew"})])
frame = ttk.Frame(self, style = "RoundedFrame", padding = 10)
frame.pack()
entry = tk.Entry(frame, borderwidth = 0, width = 40)
entry.pack(fill="both", expand=True)
app = Application()
app.mainloop()
root = tk.Tk()
style = ttk.Style()
style.theme_use('clam')
borderImage = tk.PhotoImage("borderImage", data=borderImageData)
style.element_create("RoundedFrame", "image", borderImage, border=16, sticky="nsew")
style.layout("RoundedFrame", [("RoundedFrame", {"sticky": "nsew"})])
frame = ttk.Frame(root, style = "RoundedFrame", padding = 10)
frame.pack()
entry = tk.Entry(frame, borderwidth = 0, width = 40)
entry.pack(fill="both", expand=True)
root.mainloop()
The problem is pythons garbage collections destroying the PhotoImage instance. You need to keep a reference to it (this is also somewhere in the docs). You probably just wanna save most stuff as an attribute anyway:
class Application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.style = ttk.Style()
self.style.theme_use('clam')
self.borderImage = tk.PhotoImage("borderImage", data=borderImageData)
self.style.element_create("RoundedFrame", "image", self.borderImage, border=16, sticky="nsew")
self.style.layout("RoundedFrame", [("RoundedFrame", {"sticky": "nsew"})])
self.frame = ttk.Frame(self, style="RoundedFrame", padding=10)
self.frame.pack()
self.entry = tk.Entry(self.frame, borderwidth=0, width=40)
self.entry.pack(fill="both", expand=True)
Works for me.

Tkinter text widget is wider than any other widget with grid

From tutorials, I have kind of been under the impression that grid just "kind of figures it out" for width, but it's clearly not the case when it comes to Text (I suppose when combined with list).
With the following code, listbox has is tiny and Text is absolutely massive (Width wise). I can make the listbox equal to the size of the Text by changing sticky="ew", but that's not what I want - I want a reasonable, equivalently styled "grid".
If I hardcode the size of the width, it's even more frustrating, because listbox width seems to equate to approximately 2/3 of Text width.
I've read up on rowconfigure and columnconfiugre, but this seems to actually do nothing with the below code (note - rowconfigure and columnconfigure are not in the below code, but I have tried them, perhaps I'm using them wrong).
Anyways, with the below code - can anyone explain to me how to make these more reasonably sized width wise, and also the same? Should I hardcode a width to Text and then set listbox to sticky="ew"? Seems counter intuitive to the grid layout concept.
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
self.frame = tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.initialize()
def initialize(self):
self.lst_bx = tk.Listbox(self.parent, height = 15)
self.lst_bx.grid(row=0, column=0, columnspan=2, padx=(10,10), sticky="w")
self.exe_field = tk.Text(self.parent, height=15)
self.exe_field.grid(row=1, column=0, columnspan=2, padx=(10,10), sticky="w")
self.pick_exe_btn = tk.Button(
self.parent, text="Choose Location", width=15
)
self.pick_exe_btn.grid(row=0, column=2)
if __name__ == "__main__":
root = tk.Tk()
#window_config(root)
MainApplication(root).grid(stick="nesw")
root.resizable(False, False)
root.mainloop()
I'm sorry for being such a noob, I swear I have searched this a lot before posting here - I just cannot find a straight answer on why ListBox is a completely different width from Text (and even moreso when I specify a width).
The how tkinter calculates the dimensions tkinter.ListBox() and tkinter.Text() widgets is a little bit different from most of other widgets where it uses pixels. For these 2 specific widgets, the width is calculated in terms of characters whereas the height is assessed in terms of lines.
You did not set the width of self.lst_bx, so tkinter sets it by default to 20 characters.
You did not specify the width of self.exe_field either. So tkinter calculates this width based on the current default font size.
From 1) and 2) we can conclude that it is quite normal we can not expect from self.exe_field to have the same width as self.lst_bx. This means, you have no option other than hard coding them and visually check the GUI.
With minor changes of your code (mainly provided in the comments below your question) this is how I solved your issue:
import tkinter as tk
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.initialize()
def initialize(self):
self.lst_bx = tk.Listbox(self, height = 15, width=70)
self.lst_bx.grid(row=0, column=0, padx=(10,10), sticky="w")
self.exe_field = tk.Text(self, height=15, width=80)
self.exe_field.grid(row=1, column=0, padx=(10,10), sticky="w")
self.pick_exe_btn = tk.Button(self, text="Choose Location", width=15)
self.pick_exe_btn.grid(row=0, column=2, columnspan=2)
if __name__ == "__main__":
root = tk.Tk()
#window_config(root)
MainApplication(root).grid(stick="nesw")
root.resizable(False, False)
root.mainloop()
Note where I moved the columnspace option (it quite does not make a sens in the two places where you have set it previously). Note also that tkinter calculate the character somehow differently between the listbox and text widgets. Depending your operating system (and maybe machine also), you may have to change the 2 width dimensions I set to the widgets in questions.
Here is what I got on my machine:

Tkinter Python managing scrollbar inside the listbox in a canvas

I got the code below which contain a listbox inside a canvas, I'm trying to place the scrollbar only inside the listbox but what I've got is the scrollbar always set on the whole form. Can you pls help me to put the scrollbar inside the listbox only and not on the whole canvas.
def __init__(self):
self.form = Tk()
self.form.title("Admin");
self.form.geometry('608x620+400+50')
self.form.option_add("*font",("Monotype Corsiva",13))
self.form.overrideredirect(True)
self.form.resizable(width=FALSE, height=FALSE)
self.canvas = Canvas(self.form)
self.canvas.pack(expand=YES,fill=BOTH)
self.photo = PhotoImage(file='src/back3.gif')
self.canvas.create_image(-7,-8,image=self.photo,anchor=NW)
self.scrollbar = tk.Scrollbar(self.canvas, orient="vertical")
self.lb = tk.Listbox(self.canvas, width=78, height=15,yscrollcommand=self.scrollbar.set)
self.lb.place(relx=0.04,rely=0.17)
self.scrollbar.config(command=self.lb.yview)
self.scrollbar.pack(side="right", fill="none")
I suggest you to create a Frame to wrap the Listbox and the Scrollbar. Here it is a class I wrote to do the same thing, it does not fit exactly with your code - I use grid() instead of pack() -, but you get the idea.
class ScrollableListbox(tk.Listbox):
def __init__(self, master, *arg, **key):
self.frame = tk.Frame(master)
self.yscroll = tk.Scrollbar(self.frame, orient=tk.VERTICAL)
tk.Listbox.__init__(self, self.frame, yscrollcommand=self.yscroll.set, *arg, **key)
self.yscroll['command'] = self.yview
def grid(self, *arg, **key):
self.frame.grid(*arg, **key)
tk.Listbox.grid(self, row=0, column=0, sticky='nswe')
self.yscroll.grid(row=0, column=1, sticky='ns')

Expandable and contracting frame in Tkinter

Does anyone know if there is already a widget/class to handle expanding/contracting a frame based on a toggled button (checkbutton) in tkinter/ttk?
This question stems from my attempt to clean up a cluttered gui that has lots of options categorized by specific actions. I would like something along the lines of:
example found on google
However instead of just text, allow for buttons, entries, any of tkinter's widgets. If this doesn't already exist, would it be possible/useful to create a class that inherits the tkinter Frame:
import tkinter as tk
import ttk
class toggledFrame(tk.Frame):
def __init__(self):
self.show=tk.IntVar()
self.show.set(0)
self.toggleButton=tk.Checkbutton(self, command=self.toggle, variable=self.show)
self.toggleButton.pack()
self.subFrame=tk.Frame(self)
def toggle(self):
if bool(self.show.get()):
self.subFrame.pack()
else:
self.subFrame.forget()
Note: this code is untested, just presenting concept
I am actually surprised at how close I was to getting functioning code. I decided to work on it some more and have develop a simple little class to perform exactly what I wanted (comments and suggestions on the code are welcome):
import tkinter as tk
from tkinter import ttk
class ToggledFrame(tk.Frame):
def __init__(self, parent, text="", *args, **options):
tk.Frame.__init__(self, parent, *args, **options)
self.show = tk.IntVar()
self.show.set(0)
self.title_frame = ttk.Frame(self)
self.title_frame.pack(fill="x", expand=1)
ttk.Label(self.title_frame, text=text).pack(side="left", fill="x", expand=1)
self.toggle_button = ttk.Checkbutton(self.title_frame, width=2, text='+', command=self.toggle,
variable=self.show, style='Toolbutton')
self.toggle_button.pack(side="left")
self.sub_frame = tk.Frame(self, relief="sunken", borderwidth=1)
def toggle(self):
if bool(self.show.get()):
self.sub_frame.pack(fill="x", expand=1)
self.toggle_button.configure(text='-')
else:
self.sub_frame.forget()
self.toggle_button.configure(text='+')
if __name__ == "__main__":
root = tk.Tk()
t = ToggledFrame(root, text='Rotate', relief="raised", borderwidth=1)
t.pack(fill="x", expand=1, pady=2, padx=2, anchor="n")
ttk.Label(t.sub_frame, text='Rotation [deg]:').pack(side="left", fill="x", expand=1)
ttk.Entry(t.sub_frame).pack(side="left")
t2 = ToggledFrame(root, text='Resize', relief="raised", borderwidth=1)
t2.pack(fill="x", expand=1, pady=2, padx=2, anchor="n")
for i in range(10):
ttk.Label(t2.sub_frame, text='Test' + str(i)).pack()
t3 = ToggledFrame(root, text='Fooo', relief="raised", borderwidth=1)
t3.pack(fill="x", expand=1, pady=2, padx=2, anchor="n")
for i in range(10):
ttk.Label(t3.sub_frame, text='Bar' + str(i)).pack()
root.mainloop()
This code produces:
To my knowledge, Tkinter/ttk does no provide such widgets. You might mimic your example (expand/collapse label list) with a tkinter.ttk.Treeview.
It is perfectly acceptable1 to develop your own widgets, and your code seems a right start.

scrollable listbox within a grid using tkinter

I'm new to this place and tkinter. I am stuck at making a scrollable listbox or canvas. I have tried both widgets. Within this listbox or canvas, I have several entry and label widgets. The origin point is R0,C0. I used row/columnconfigure to stretch the listbox or canvas.
In the main window, I had 4 buttons on row four to column four (0,4->4,4). I placed the scrollbar on column 5. I attempted to use the grid method. The issue I am having is making the scrollbar functional.
Note: Turning the mainframe into a class is only one of the ways I have tried. Packing the scrollbar on the right has worked, with the listbox/canvas packed on the left. However, the listbox/canvas widget that the scrollbar is commanded to does not scroll the listbox/canvas. Also, adding many entry boxes does not cause the listbox/canvas to scroll. Help please.
from tkinter import *
from tkinter.ttk import *
Style().configure("B.TFrame", relief="flat",
background="blue")
Style().configure("R.TFrame", relief="flat",
background="red")
Style().configure("R.TLabel", background="red")
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master, style="B.TFrame")
self.grid(sticky=N+S+E+W)
self.mainframe()
def mainframe(self):
top=self.winfo_toplevel()
self.menuBar = Menu(top)
top["menu"] = self.menuBar
self.subMenu = Menu(self.menuBar, tearoff=0)
self.subMenu2 = Menu(self.menuBar, tearoff=0)
self.menuBar.add_cascade(label="File", menu=self.subMenu)
self.menuBar.add_cascade(label="About", menu=self.subMenu2)
self.subMenu.add_command(label="Open")
self.subMenu.add_command(label="Save")
self.subMenu.add_command(label="Exit")
self.subMenu2.add_command(label="About")
self.subMenu2.add_command(label="Help")
self.data = Listbox (self, bg='red')
scrollbar = Scrollbar(self.data, orient=VERTICAL)
self.add = Button(self, text="")
self.remove = Button(self, text="")
self.run = Button(self, text="")
self.stop = Button(self, text="")
self.data.grid (row=0, column=0, rowspan=4, columnspan=4, sticky=N+E+S+W)
self.data.columnconfigure(1, weight=1)
self.data.columnconfigure(3, weight=1)
self.add.grid(row=4,column=0,sticky=EW)
self.remove.grid(row=4,column=1,sticky=EW)
self.run.grid(row=4,column=2,sticky=EW)
self.stop.grid(row=4,column=3,sticky=EW)
scrollbar.grid(column=5, sticky=N+S)
Without any content in the listbox, there's nothing to scroll...
This seems to work though (shortened the example a bit). See also the example at the scrollbar documentation.
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.grid(sticky=N+S+E+W)
self.mainframe()
def mainframe(self):
self.data = Listbox(self, bg='red')
self.scrollbar = Scrollbar(self.data, orient=VERTICAL)
self.data.config(yscrollcommand=self.scrollbar.set)
self.scrollbar.config(command=self.data.yview)
for i in range(1000):
self.data.insert(END, str(i))
self.run = Button(self, text="run")
self.stop = Button(self, text="stop")
self.data.grid(row=0, column=0, rowspan=4,
columnspan=2, sticky=N+E+S+W)
self.data.columnconfigure(0, weight=1)
self.run.grid(row=4,column=0,sticky=EW)
self.stop.grid(row=4,column=1,sticky=EW)
self.scrollbar.grid(column=2, sticky=N+S)
a = Application()
a.mainframe()
a.mainloop()
You must define the command attribute to the scrollbar, and you must supply the yscrollcommand attribute to the listbox. These two attributes work together to make something scrollable.
The yscrollcommand option tells the listbox "when you are scrolled in the Y direction, call this command. This is usually the set method of a scrollbar, so that when the user scrolls via arrow keys, the scrollbar gets updated.
The command attribute of a scorllbar says "when the user moves you, call this command". This is usually the yview or xview method of a widget, which causes the widget to change its view parameters in the Y or X direction.
In your case, after creating the widgets you would do this:
self.data.config(yscrollcommand=self.scrollbar.set)
scrollbar.config(command=self.data.yview)
This thread is old but in case somebody else falls across it as I did, it needs a few precisions.
Junuxx's answer doesnt work as is, not only because there is an indentation problem due to difficulties in seizing code here (from "self.run" which is part of the "mainframe" function) but because it seems necessary to put the listbox and the scrollbar in their own frame.
Here is a working code for Python 2 and 3 :
#!/usr/bin/env python2
try:
# for Python2
from Tkinter import *
except ImportError:
# for Python3
from tkinter import *
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.grid(sticky=N+S+E+W)
self.mainframe()
def mainframe(self):
frame = Frame(self)
scrollbar = Scrollbar(frame, orient=VERTICAL)
data = Listbox(frame, yscrollcommand=scrollbar.set,
bg='red')
scrollbar.config(command=data.yview)
scrollbar.pack(side=RIGHT, fill=Y)
data.pack(side=LEFT, fill=BOTH, expand=1)
for i in range(1000):
data.insert(END, str(i))
self.run = Button(self, text="run")
self.stop = Button(self, text="stop")
frame.grid(row=0, column=0, rowspan=4,
columnspan=2, sticky=N+E+S+W)
frame.columnconfigure(0, weight=1)
self.run.grid(row=4,column=0,sticky=EW)
self.stop.grid(row=4,column=1,sticky=EW)
a = Application()
a.mainframe()
a.mainloop()
You may find further information here : https://www.effbot.org/tkinterbook/listbox.htm.
Hope this helps.

Categories

Resources