Here is my Tkinter code. I intend to show the both horizontal and vertical scrollbar.
from Tkinter import *
root = Tk()
scrollbar = Scrollbar(root)
scrollbar.pack(side = RIGHT,fill = Y)
scrollbar_x = Scrollbar(root)
scrollbar_x.pack(side = BOTTOM,fill = X)
mylist = Listbox(root,xscrollcommand = scrollbar_x.set,yscrollcommand = scrollbar.set)
"""
To connect a scrollbar to another widget w, set w's xscrollcommand or yscrollcommand to the scrollbar's set() method. The arguments have the same meaning as the values returned by the get() method.
"""
for line in range(100):
mylist.insert(END,("this is line number"+str(line))*100)
mylist.pack(side = LEFT,fill=BOTH)
scrollbar.config(command = mylist.yview)
scrollbar_x.config(command = mylist.xview)
print root.pack_slaves()
mainloop()
And it doesn't match up to my expectation in my computer, which is Mac Sierra.
Furthermore, what's scrollbar.config(command = mylist.yview) means?
config()
More details about this function will be helpful~ thanks
Tkinter program: doesn't show me the bottom scrollbar
I can't duplicate that when I run your code. When I run your code I see a giant scrollbar at the bottom of the screen.
I presume you want the "x" scrollbar to be horizontal rather than vertical. If that is true, you must tell tkinter that by using the orient option:
scrollbar_x = Scrollbar(root, orient="horizontal")
Furthermore, what's scrollbar.config(command = mylist.yview) means?
Scrollbars require two-way communication. The listbox needs to be configured to know which scrollbar to update when it has been scrolled, and the scrollbar needs to be configured such that it knows which widget to modify when it is moved.
When you do scrollbar.config(command=mylist.yview) you are telling the scrollbar to call the function mylist.yview whenever the user tries to adjust the scrollbar. The .yview method is a documented method on the Listbox widget (as well as a few others). If you do not set the command attribute of a scrollbar, interacting with the scrollbar will have no effect.
Related
I'm creating a GUI in tkinter and I'm trying to customize the window header so I can change the colour.
I've used:
root.overrideredirect(True)
to get rid of the header, then rebuilt the function of moving the tab with:
def move_window(event):
x, y = root.winfo_pointerxy()
root.geometry(f"+{x-650}+{y}")
title_bar = tk.Frame(root, bg=pallete["pallete1"], bd=0,height=22)
title_bar.config(highlightthickness=2, highlightcolor= pallete["pallete3"])
title_bar.pack(fill="x")
title_bar.bind('<B1-Motion>', move_window)
Only this is, that when I overrideredirect to get rid of the window header, the program disappears in the taskbar, so you can't find it.
I'm just wondering if there is a way to get around this or to change the window header without having to remove it and rebuild a new one.
def minsize(): # minsize func() for your button
root.overrideredirect(0) # minsize window and iconify
root.iconify()
def showwindow(event):
root.overrideredirect(1)
root.iconify()
bind this to your heading bar with map
your_widdgget.bind("<Map>", lambda event: showindow(event)) # in this case i don't know why lambda but it still works for me
Im coding my personal text editor. But i have a problem with the 2 widget text and the scrollbar (connect one scrollbar to two text).
What is my idea and logic (at the beginning)?
I want to display 2 text, one for writing text entered by user, and one to display the number of the line. I pack both of them, in the root. Then i create a scrollbar, that will scroll on Y axes the 2 text, so what i want to do (mainly) is to connect 2 widget (text) to one scrollbar.
But it didn't work.
This system absolutely doesn't work, are there any suggest or fix to fix this first idea?
Other ideas that i found.
After the first attempt, i thought that i can pack the 2 texts into 1 container. I tried to create a frame (packed into root) that contains the 2 texts, i did this because i have to connect the scrollbar only to the frame. But it didn't work, moreover it didnt allow me to write the following snippet: command=frame.yview in the scrollbar option, it seems that i cant connect frame to scrollbar.
So:
I will ask u if my reasoning are good, and how to solve. If not what can i do?
Similar question found on Google: (but that i dont undestand)
How to scroll two parallel text widgets with one scrollbar?
Tkinter adding line number to text widget
from tkinter import *
root = Tk()
root.geometry("480x540+100+100")
root.config(cursor='')
line = Text(root, bg="light grey", font="Roman 24", width=4)
line.pack(side=LEFT, fill=BOTH)
text = Text(root, bg="grey", font="Roman 24")
text.pack(side=LEFT, fill=BOTH, expand=True)
scrollbar = Scrollbar(text, orient=VERTICAL, command=(line.yview, text.yview))
text.configure(yscrollcommand=scrollbar.set)
line.configure(yscrollcommand=scrollbar.set)
scrollbar.pack(side=RIGHT, fill=Y)
for n in range(50):
line.insert("{}.0".format(n+1), "{}\n".format(n+1))
text.insert("{}.0".format(n+1), "Line no. {}\n".format(n+1))
if __name__ == '__main__':
root.mainloop()
There's nothing special about a scrollbar - it just calls a function when you move it. The API for this function is well defined. While it normally should call the yview (or xview) method of a scrollable window, there's no requirement that it must.
If you want to control two widgets with a single scrollbar, create a function for your scrollbar that scrolls both windows.
def multiple_yview(*args):
line.yview(*args)
text.yview(*args)
scrollbar = Scrollbar(text, orient=VERTICAL, command=multiple_yview)
You will have a similar problem when you scroll the text widget while entering new lines or moving around with cursor keys. You'll need to configure the yscrollcommand attribute of the text widget to call a function so that it both updates the scrollbar and also scrolls the other window (and maybe also add additional line numbers)
Hi I can't seem to find the cause of a weird issue I am having in Tkinter\Python
When using the following code to create a scrolling window on a canvas
myFrame = Frame(ob)
ob.create_window((0, 0), window=myFrame, anchor='nw')
scroll = Scrollbar(sub, orient="vertical", command=ob.yview)
ob.configure(yscrollcommand=scroll.set)
scroll.grid(row=0, column=1, sticky=N+S)
# add check boxes for clients to buttons
for row in clients:
v = IntVar()
item = Checkbutton(myFrame, text=row[1], variable=v)
item.va = v
item.grid(sticky='w')
clientlist[str(row[0])] = v
ob.configure(scrollregion=ob.bbox('all'))
It creates the windows inside of the canvas and also creates the items inside of it. It even creates the scrollbar next to the canvas just fine.
The issue is that when I scroll down it allows me to scroll forever. It also scrolls upwards just past the top most item in the canvas window and then disables the scroll function.
Any insight on this as it seems I must not be setting the scroll region correctly is my guess but I am not sure what is wrong with it.
Here's my program:
import tkinter as tk
#Create main window object
root = tk.Tk()
#build GUI
for i in range(5):
tk.Label(root, text="hello", height=0).grid(row=i)
#mainloop
root.mainloop()
It produces the following (running in Xubuntu 16.04 LTS)
Notice all that extra vertical space between the lines of text. I don't want that! How do I decrease it?
If I run this code instead:
import tkinter as tk
#Create main window object
root = tk.Tk()
#build GUI
for i in range(5):
tk.Label(root, text="hello", height=0).grid(row=i)
tk.Grid.rowconfigure(root, i, weight=1) #allow vertical compression/expansion to fit window size after manual resizing
#mainloop
root.mainloop()
...it opens up and initially looks exactly the same as before, but now I can manually drag the box vertically to shrink it, like so:
Notice how much more vertically-compressed it is! But, how do I do this programatically, so I don't have to manually drag it to make it this way? I want to set this tight vertical spacing from the start, but no matter which parameters I change in Label, grid, or rowconfigure I can't seem to make it work without me manually dragging the box with the mouse to resize and vertically compress the text.
There are many ways to affect vertical spacing.
When you use grid or pack there are options for padding (eg: pady, ipady, minsize). Also, the widget itself has many options which control its appearance. For example, in the case of a label you can set the borderwidth, highlightthickness and pady values to zero in order to make the widget less tall.
Different systems have different default values for these various options, and for some of the options the default is something bigger than zero. When trying to configure the visual aspects of your GUI, the first step is to read the documentation, and look for options that affect the visual appearance. Then, you can start experimenting with them to see which ones give you the look that you desire.
In your specific case, this is about the most compact you can get:
label = tk.Label(root, highlightthickness=0, borderwidth=0, pady=0, text="hello")
label.grid(row=i, pady=0, ipady=0)
You can programatically modify the geometry just before starting the main loop instead of manually dragging it (change 0.6 to whatever % reduction you want):
import tkinter as tk
#Create main window object
root = tk.Tk()
#build GUI
for i in range(5):
label = tk.Label(root, text = 'hello')
label.grid(row=i)
tk.Grid.rowconfigure(root, i, weight=1) #allow vertical compression/expansion to fit window size after manual resizing
#mainloop
root.update()
root.geometry("{}x{}".format(root.winfo_width(), int(0.6*root.winfo_height())))
root.mainloop()
Here is a screenshot of the result running on Xubuntu 16.04 LTS with Python 3.5.2:
I have a fairly large size GUI built in tkinter using the grid manager method. I need to add a scrollbar to the entire window and Im having some trouble. Here is a very crude version of a scroll-bar using the grid manager in Tkinter (i dont want to scroll the list box, i want to scroll the entire Tk window).
import Tkinter
from Tkinter import *
Tk = Tkinter.Tk
self=Tk()
listbox = Listbox(self, width = 10, height = 60)
listbox.grid(row =0, column=0)
scrollbar = Scrollbar(self)
scrollbar.grid(sticky=E, row = 0, rowspan = 100, column = 11, ipady = 1000)
mainloop()
Is it possible to fix the Tkinter window size (using grid manager) and add a scrollbar which then allows the user to view additional content? The window is too large and additional content needs to be viewed so the only option i see is a scrollbar. I only see examples using the pack method. As you can probably guess I am new to Tkinter and would appreciate any input.
Thanks to all in advance.
You cannot scroll the entire contents of a root window, a Toplevel window, or a Frame. The solution is to put all of your widgets in a canvas, and then add a scrollbar to the canvas. There are questions on this site that give examples, such as Python Tkinter scrollbar for frame