How to Design the Scrollbar in tkinter (not ttk ones) - python

I have tried to design the tkinter's Scrollbar instead of using ttk ones.
But I couldn't be able to implement designs on the scrollbar. I think I have implemented the same config options as in the effbot docs.
from tkinter import *
testing=Tk()
a=Listbox(testing,height=6)
sc=Scrollbar(testing,orient=VERTICAL,command=a.yview)
for i in range(100):
a.insert(END,i)
a.config(yscrollcommand=sc.set)
def designs():
# This function does nothing :(
sc.config(background='orange',borderwidth=34)
sc.config(highlightthickness=30,highlightcolor='orange',highlightbackground='skyblue')
sc.config(troughcolor='orange')
designs() # here...
sc.pack(side=RIGHT,fill=Y)
a.pack()
testing.mainloop()
is there a way to change the background color of the trough part and non-trough part of the scrollbar and some effects like the active background? Please Help!!!

Related

How to smoothly go from tkinter widgets to tkinter.ttk widgets without much of a hassle [Closed, But could answer if wanted]

I have coded a tkinter application using the usual tk widgets. I have learned that the tkinter.ttk widgets have a more modern look and make everything better. But now alot of my code does not work. Here is a small one which I don't understand why.
Here is tkinter
And tkinter.ttk
The difference is Type of Project is not there... I got around this by just putting Type of Project in the options it doesn't show it either(But how would I be able to like preset it).
Here is tk code:
om1str = StringVar()
om1str.set('Type of Project')
om1list = ['Photo Editing', 'Video Editing']
om1 = OptionMenu(new, om1str, *om1list, command = Create_New.more_options)
And ttk code:
om1str = StringVar()
om1list = ['Type of Project', 'Photo Editing', 'Video Editing']
om1 = OptionMenu(new, om1str, *om1list, command = Create_New.more_options)
Here is another example. Before swapping from tkinter to tkinter.ttk this piece of code worked but now does not. (I had this exact same code for the photo editing option two)
if choice == 'Video Editing':
print('hi')
for widgets in new.winfo_children():
if widgets.winfo_class() == 'Frame' or widgets.winfo_class() == 'Button':
widgets.destroy()
And now this kinda happens (The name does not duplicate because it is not in the same def Create_New.more_options())
How would I be able to delete the frame and the button without removing all contents of the tk.Toplevel()
I know how to set the bg and fg color(was really confused) after some quick google searches.
Is there any smooth way to go from tkinter to the tkinter.ttk widget styles without having to do a little 2 much work.

fill attribute of pack in tkinter is not working

I am trying to keep the title in centre when resizing the window
I tried to change the label and to import * modules from tkinter for possible solution
Any help or hint is highly appreciated
following is the attached code:
import tkinter as t
root=t.Tk()
root.geometry("1000x500")
'''Login/Register page '''
root.title("LOGIN PAGE")
bankLabel=t.Label(text="Bank of DINAGOD")
bankLabel.pack(fill='x')
bankLabel.place(x=475)
root.mainloop()
You can't use both pack() and place() function on the same widget. Use only pack() function to declare the positions of your widget.

Is there different ways to create a menu bar?

I've come across two different ways to create a menu bar:
import tkinter as tk
window = tk.Tk()
menu_bar = tk.Menu() ■
window.config(menu = menu_bar) #2
window.mainloop()
import tkinter as tk
from tkinter import Menu
window = tk.Tk()
menu_bar = Menu(window) ■
window.configure(menu = menu_bar)
window.mainloop()
Question: what's the difference between these lines of code? By this I mean, why is the syntax different If they do the same? (I've marked the referred lines of code as ■). How importing Menu from tkinter affect the lines of code?
What's the difference between these lines of code?
It does the same thing(except the imports. With from tkinter import Menu, you are specifically just importing Menu and nothing else from tkinter. But in the first example, you are importing whole tkinter and you can refer to tkinter.Menu as tk.Menu. But in the second example, you just have to say Menu.
Note that in the second example you can still use tk.Menu as well as Menu. So the second import is rendered useless, and can be removed. It is better to follow the first example.
As mentioned by AST, if you say Menu(), an existing instance of Tk() will be passed as the master argument implicitly. But if you say Menu(win), you are passing win explicitly. It is always recommended to pass the parent argument explicitly while you work with multiple windows so as to not cause confusions.

Why does the delete method not free up memory for a widget in Tkinter?

In Tkinter I have a large block of code that updates and clears a ScrolledText widget. I noticed during testing however that there was a memory leak regarding this, and after narrowing it down the simplest way to replicate this behavior is:
import tkinter
import tkinter.scrolledtext
def meme(box_text):
while True:
box_text.insert("end", "meme")
box_text.delete(1.0, "end")
window_main = tkinter.Tk()
box_text = tkinter.scrolledtext.ScrolledText(window_main)
button = tkinter.Button(window_main, command=lambda: meme(box_text))
box_text.pack()
button.pack()
window_main.mainloop()
Can anyone explain what I'm doing wrong? I don't understand nor see the problem with this in relation to memory. If I comment out the insert, with only the delete, there's no memory issue so the loop itself is not to blame as far as I can tell, only the insert.
I found the culprit. For the Text widget by default "maxundo" is set to 0, and thus Tkinter keeps a log of everything inserted into it. Setting this to 1 solved the problem. Edit: undo=False works better.

Notebook widget in Tkinter

Having played around a little with both Tkinter and wxPython, I like Tkinter much better in terms of how clean my source code looks. However, it doesn't seem to have as many features; in particular it doesn't have tabs (as in, the tabs at the top of a Firefox window).
A little Googling on the subject offers a few suggestions. There's a cookbook entry with a class allowing you to use tabs, but it's very primitive. There's also Python megawidgets on SourceForge, although this seems very old and gave me errors during installation.
Does anyone have experience making tabbed GUIs in Tkinter? What did you use? Or is it simply the case that anyone who needs more powerful windowing components has to use wxPython?
On recent Python (> 2.7) versions, you can use the ttk module, which provides access to the Tk themed widget set, which has been introduced in Tk 8.5.
Here's how you import ttk in Python 2:
import ttk
help(ttk.Notebook)
In Python 3, the ttk module comes with the standard distributions as a submodule of tkinter.
Here's a simple working example based on an example from the TkDocs website:
from tkinter import ttk
import tkinter as tk
from tkinter.scrolledtext import ScrolledText
def demo():
root = tk.Tk()
root.title("ttk.Notebook")
nb = ttk.Notebook(root)
# adding Frames as pages for the ttk.Notebook
# first page, which would get widgets gridded into it
page1 = ttk.Frame(nb)
# second page
page2 = ttk.Frame(nb)
text = ScrolledText(page2)
text.pack(expand=1, fill="both")
nb.add(page1, text='One')
nb.add(page2, text='Two')
nb.pack(expand=1, fill="both")
root.mainloop()
if __name__ == "__main__":
demo()
Another alternative is to use the NoteBook widget from the tkinter.tix library. To use tkinter.tix, you must have the Tix widgets installed, usually alongside your installation of the Tk widgets. To test your installation, try the following:
from tkinter import tix
root = tix.Tk()
root.tk.eval('package require Tix')
For more info, check out this webpage on the PSF website.
Note that tix is pretty old and not well-supported, so your best choice might be to go for ttk.Notebook.
If anyone still looking, I have got this working as Tab in tkinter. Play around with the code to make it function the way you want (for example, you can add button to add a new tab):
from tkinter import *
class Tabs(Frame):
"""Tabs for testgen output"""
def __init__(self, parent):
super(Tabs, self).__init__()
self.parent = parent
self.columnconfigure(10, weight=1)
self.rowconfigure(3, weight=1)
self.curtab = None
self.tabs = {}
self.addTab()
self.pack(fill=BOTH, expand=1, padx=5, pady=5)
def addTab(self):
tabslen = len(self.tabs)
if tabslen < 10:
tab = {}
btn = Button(self, text="Tab "+str(tabslen), command=lambda: self.raiseTab(tabslen))
btn.grid(row=0, column=tabslen, sticky=W+E)
textbox = Text(self.parent)
textbox.grid(row=1, column=0, columnspan=10, rowspan=2, sticky=W+E+N+S, in_=self)
# Y axis scroll bar
scrollby = Scrollbar(self, command=textbox.yview)
scrollby.grid(row=7, column=5, rowspan=2, columnspan=1, sticky=N+S+E)
textbox['yscrollcommand'] = scrollby.set
tab['id']=tabslen
tab['btn']=btn
tab['txtbx']=textbox
self.tabs[tabslen] = tab
self.raiseTab(tabslen)
def raiseTab(self, tabid):
print(tabid)
print("curtab"+str(self.curtab))
if self.curtab!= None and self.curtab != tabid and len(self.tabs)>1:
self.tabs[tabid]['txtbx'].lift(self)
self.tabs[self.curtab]['txtbx'].lower(self)
self.curtab = tabid
def main():
root = Tk()
root.geometry("600x450+300+300")
t = Tabs(root)
t.addTab()
root.mainloop()
if __name__ == '__main__':
main()
While it may not help you at the moment, tk 8.5 comes with an extended set of widgets. This extended set is available with tk 8.4 by way of an extension known as "tile". Included in the extended set of widgets is a notebook widget. Unfortunately, at this time Tkinter by default uses a fairly old version of Tk that doesn't come with these widgets.
There have been efforts to make tile available to Tkinter. Check out http://tkinter.unpythonic.net/wiki/TileWrapper. For another similar effort see http://pypi.python.org/pypi/pyttk. Also, for a taste of how these widgets look (in Ruby, Perl and Tcl) see http://www.tkdocs.com/.
Tk 8.5 is a huge improvement over stock Tk. It introduces several new widgets, native widgets, and a theming engine. Hopefully it will be available by default in Tkinter some day soon. Too bad the Python world is lagging behind other languages.
update: The latest versions of Python now include support for the themed widgets out of the box. _
"Or is it simply the case that anyone who needs more powerful windowing components has to use wxPython?"
Short answer: yes.
Long answer:
It may take some practice for your wxPython code to feel "clean," but it is nicer and much more powerful than Tkinter. You will also get better support, since more people use it these days.
What problems did you have with pmw? It's old, yes, but it's pure python so it should work.
Note that Tix doesn't work with py2exe, if that is an issue for you.

Categories

Resources