spreadsheet-like-app in tkinter? - python

Suppose one were to want to build a spread-sheet in tkinter. So one could have (many) cells, most of which would take text entry, but some could be drop-down lists (tkinter comboboxes) etc.
Is that feasible in tkinter for a spreadsheet of any size? (Say 100x10)? Or would that many tkinter Text object and comboboxes etc just be too much handle performance wise? (Or is there some other way to do this other than just gridding many tkinter objects?)
I ask not because I want to do this literally, but because I need to build an app in which users are presented with lots of chunks of information, any of them editable. It's convenient for the user to access those chunks directly (click on the cell) rather than have to pass through some preliminary interface.

I think the best way to know is to try. I used the code below to create a 100x10 grid of Entry widgets and tkinter did not seem slow once all widgets were created. But it will depend on the performances of the computer.
import tkinter as tk
root = tk.Tk()
for i in range(100):
for j in range(10):
tk.Entry(root)).grid(row=i, column=j)
root.mainloop()

Related

Python TKinter Is there a way to dynamically link variables and widgets to save RAM/memory

I am making a physics calculator that involves splitting and organizing my calculator by topic. this means i have lots of widgets to navigate the menu to get to the problem. i don't want to delete the widgets as this would remove all the details of the widgets which would make my code unnecessarily complicated and long. is there a way to dynamically link the variables or widgets so that the program can run more smoothly?
I do not know of any way to do this so i cannot provide any code. If there is no way to do it i would also appreciate knowing this.
a way to dynamically link the variables or widgets so that the program can run more smoothly?
I suggest taking look at StringVar which might be linked with suitable widgets, consider following simple example
from tkinter import Tk, Label, Entry, StringVar
root = Tk()
str_var = StringVar(root,"hello")
label = Label(root, textvariable=str_var)
entry = Entry(root, textvariable=str_var)
label.pack()
entry.pack()
root.mainloop()
This creates window with Label and Entry, as you change text in Entry corresponding change is made to Label. You might also find .trace method of StringVar instance useful as it allows you to register callback i.e. function to be called when value was changed.

Is it possible to style a tkinter Text widget?

I've been using the 'ttkthemes' module to add some additional user-selectable themes to my application via an OptionMenu, and all my Text widgets stand out a bit in their standard style.
I would like to know if it is at all possible to style a Tkinter Text widget with ttk themes.
I know you can't actually style the standard Tkinter widget (you instead must use the respective ttk version).
My issue is that there is no ttk.Text only the entry and I need multiple lines for a text editor.
One possibility would be to change background colour based on the selected theme, but this could be very long since there many themes to choose from.
Would anyone know of any possible way to get a stylable version of the Tkinter Text widget, so that it doesn't stand out as much?
Would anyone know of any possible way to get a stylable version of the Tkinter Text widget, so that it doesn't stand out as much?
You can create your own custom Text widget that listens to the <<ThemeChanged>> event, and then reconfigures itself based on the settings from the current theme or other widgets. For example, you could query the current theme to get the background color of an Entry widget and use that to set the background of the Text widget.

Issues with tkinter grid and pack in separate classes

So I've been working on a group project, some of use used pack and others used grid as a layout manager, I'm making the part of the application that puts everyones code together.
I've been working on a UI using pack, and what I want it to do is when I click on a button, a new tk.Tk() window is launched which then runs its code that is managed by grid.
Here is a snipped of the code to try and show you what I'm doing, I keep getting the error "cannot use the geometry manager grid inside . which already has slaves managed by pack"
def launchQuest(self, questType):
if(questType == "ham"):
ham = tk.Tk()
ham.configure(background='white')
app = HM(ham)
ham.mainloop()
If you need to see more code just ask, the whole class is around 400 lines so far but I don't think it is relevant.
Any help would be great!
Thanks!
Based on my first comment above, the answer is:
There should be only one Tk() root window. If you want other windows,
use Toplevel widget.
Only one type of positioning (grid, pack, or place) can be used at a time, within a container. Tk() gives you a window (Toplevel) which you use to contain other widgets, some of which can be containers themselves, like Frame, for example. You can pack two frames into a window, but you could not pack one frame and place another into the same window. This limitation only applies one level deep – you could place a frame, and then pack a frame inside that, and then grid inside that, if you wanted. It doesn't matter what method was used to position the container, only at the level of things directly contained by that container.

Scroll a group of widgets in Tkinter

I want to make a whole column of various widgets scrollable in a Tkinter GUI, like so:
Tkinter can only attach scrollbars to certain widgets, of which, frames are not included. Making a scollable column is a common practice in interfaces, and there should be a simple solution, but so far, all I have been able to find is this hacky example of a scrollable frame, using a canvas widget. A similar hacky solution was used in a similar stack overflow question.
Is there a commonly accepted way in Tkinter to make a column, or a group of widgets, that is scrollable?
The solution using the canvas is the commonly accepted way to solve this problem. It's really not all that hacky, and the end result can be indistinguishable from having a native scrolling container widget.
If you're making a single column, another option is to use a text widget, and use the widget's ability to embed other widgets. Insert a widget, then insert a newline, insert another widget, etc. You then get the scrolling ability for free. The only thing you need to worry about is configuring the width of the embedded windows, which isn't too hard to do.

Python tk scroll frame?

I'm creating a simple GUI app using Tkinter with Python, but I'm having problems adding a scrollbar to a single frame. The frame is visible from top to bottom on my 20" but in order to display everything on a netbook or any other low res screen it needs a scrollbar. Here's a snippet of how I thought it would work (it does with listboxes).
framelist = Tkinter.Frame(self.pmaster, relief=FLAT, borderwidth=0)
framelist.pack(fill=BOTH, padx=0, pady=0)
yscroll = Tkinter.Scrollbar(framelist, width=10, orient=VERTICAL)
yscroll.pack(fill=Y,side=RIGHT,padx=0,pady=0)
Though this doesn't work with frames apparently. Any help on this issue from you guys will be deeply appreciated!
I'm also wondering if Tkinter might be outdated. It was the only GUI interface I learned in school, but thats several years ago and it doesn't really meet my demands anymore. Surely there must be a better alternative? I'm on Ubuntu btw.
The frame widget of tkinter doesn't scroll. One solution is to put your whole frame in a canvas (as a canvas object), and attach the scrollbar to the canvas. You have to tell the canvas how big the scrollable area is, which you can do by getting the size of the frame once you've placed all the widgets in it. Though, you might want to reconsider your UI design -- scrollable frames aren't very easy to use no matter what GUI toolkit you use.
As for whether Tkinter is outdated... some say yes, some say no. There is a lot of tkinter misinformation out there so take all tkinter opinions with a grain of salt (even mine!). Tkinter continues to improve, it hasn't stagnated. If you have the luxury of using python 2.7 or greater you have access to the ttk widgets which offer platform-specific themes and additional widgets such as a notebook and hierarchical tree among others.
For alternatives you might want to check out wxPython. In my experience it seems to have considerably more bugs and quirks than tkinter, but has a lot more widgets and seems to be more popular.
I like to think that the difference between tkinter and wxPython is like the difference between Home Depot (a home improvement / lumbar yard store) and Ikea (prefabricated furniture that you assemble yourself) - one gives you all the bits and pieces to make just about anything you want (tkinter) the other gives you a lot of pre-packaged stuff. Each approach has its strengths and weaknesses.

Categories