The title is amusing , I know. Its like movie "Inception" situation .Dream in a dream in a dream .
Hi everyone ,I am using wxpython to make an app.
I want to achieve something like this .(This is just idea of the problem)
func1(self,event):
some widgets added
func2
func2(self,event):
remove previous widgets (by using event.GetEventObject().GetParent().Hide())
add new ones
func3
func3(self,event):
remove widgets added in func2
recall widgets added in func1 (event.GetEventObject().GetParent().Show())
I have traversed too much inside , like 3 times jumping from functions to other nested
functions that i have no track of event .
If i store the ids of events (in a list) with
event.GetId()
while traversing in every function , is there a way if i can use those ids to
replace
event.GetEventObject().GetParent().Show()
event.GetEventObject().GetParent().Hide()
with
id1.GetEventObject().GetParent().Hide()
id2.GetEventObject().GetParent().Show()
I just want to know this kind of thing is possible ?
Or theres a better simpler way ?
If any still is in such trouble , I found some ways...
First of all , you don't need to use IDs for this.....Much simpler way is
create different panels........
When you want to hide one , you can simply do
panelname.Hide()
When you want to show another ,
panelname.Show() # Or this also works (event.GetEventObject().GetParent().Show()\Hide())
Note : 1) Remember to give all widgets (buttons etc) individual IDs (or -1 will do).
2) You can also create your overlapping panels with a function call too.(example - After a button is clicked).
3) Also you can create a common panel ( a parent panel ) and then create child panels and in the init body . And then you can simply hide and show these panels in a function binded (may be with a button etc) .
I solved my problem like this . Did'nt need to use GetId() . But there might be others ways. So do share if you have some other idea.I hope this helps someone someday...Thank you all.
Related
I m creating a GUI for a simple quiz and I want my answer to be checked in radiobutton . But the problem is that I can not mark or unmark it , its always set up marked . my code is
R1=Radiobutton(win,text= 'option1',command =sel1)
My function sel1 is as follow :
def sel1():
global ans
ans = 1
As far as I see from the docs the radiobutton is used to select one out of many, whcih as I understand will always require one button to be marked. It's possible you need CheckButton.
EDIT: As it turned out the problem was to set common variable for all radio buttons as mentioned in the docs linked above.
I'm creating a GUI application in Python 2.7 using Tkinter.
I have this piece of code:
vis=Label(pur,text='Purchase Added successfully',font=(8))
vis.place(x=150,y=460)
I wanna know if there's any way to display the Label 'Purchase Added Successfully' for a limited amount of time(~3 seconds) and then it would disappear. This is because I'm interested in adding a new 'purchase' after the current one, and don't want the success messages to overlap.
There are many ways depending on the project pattern, all based on the syntax :
vis=Label(pur,text='Purchase Added successfully',font=(8))
vis.place(x=150,y=460)
vis.after(3000, function_to_execute)
Total Destruction
If you don't want to wonder whether the label is already created, hidden or empty, and mostly avoid possible memory leaks (thanks to Bryan Oakley comment):
vis.after(3000, lambda: vis.destroy() )
But then you need to create a fresh new Label for every purchase.
Hide and Seek
The following method allows to disable the display of the Label without destroying it.
vis.after(3000, lambda: vis.place_forget() )
#vis.after(3000, lambda: vis.grid_forget() ) # if grid() was used
#vis.after(3000, lambda: vis.pack_forget() ) # if pack() was used
Then you can enable it again for the next purchase, with vis.place(x=150,y=460)
Text Eraser
Another way, maybe less interesting, unless you prefer keeping an empty Label in the container widget:
vis.after(3000, lambda: vis.config(text='') )
(Note that you can replace the text with vis.config(text='blabla') for the next purchase)
I have to make a program with a text based menu system for a school project. It includes a main menu and multiple sub-menus. I have made my current version of the menu with LOTS of if-statements, prints and inputs. But it is neither a elegant nor easy solution. I was hoping there was a easier / better way to create a menu.
To be more precise, I need a method of calling the menu (to display it) after which I can choose an option, which then executes some code.
The structure looks as follows.
Input data Filters
Filters
Add filter
Type 1
Type 2
Type 3
Remove filter
Edit filter.
Do calculations
Mean
Standard variation
etc.
Create plot
Exit
The easiest way to handle this kind of problem is by recursion. Once you
establish a useful data structure it can be handled recursively quite easily, since the job of creating a sub-menu is essentially the same as creating a menu.
In the solution below I define the menu as a tuple. The items in the menu are either the commands, represented as strings, or sub-menus, represented by nested tuples.
menu_data = (
"Input data filters",
("Filters",
("Type 1",
"Type 2",
"Type 3"),
"Remove Filter",
"Edit Filter"),
"Do Calculations",
("Mean",
"Std Deviation",
"etc"),
"Create Plot",
"Exit"
)
def make_menu(menu, indent=0):
for item in menu:
if type(item) == tuple:
make_menu(item, indent+1)
else:
print(" "*indent+item)
make_menu(menu_data)
This should print the structure you require.
Use the plot functions to plot menu's. You can import menus that are already made and then modify them so they suit what you want. Ploted windows's can interact with your mouse and and keyboard to move through the menu and select a locations in it. You can make it look like a text menu, with the only diference it is in a window instead of a shell.
The library "pygame" has a lot of this.
I hope this helps.
I'm reading Programming Python by Mark Lutz, and I came across this bit of code that I don't get.
buttons = []
def onpress(i):
global state
state = i
for btn in buttons:
btn.deselect()
buttons[i].select()
I get what it's doing, but I don't get where these select and deselect methods are coming from. I've never seen these list methods before (I and the book are using Python 3). Are these builtin methods? And what do they do? I tried using my Google-fu to figure it out, but to no avail.
http://www.java2s.com/Tutorial/Python/0360__Tkinker/Deselectforradiobuttonssimplysetsthebuttonsassociatedvaluetoanullstring.htm
So a quick google, if you are doing this it means you are most likely using tkinter in which case you have missing relevant code somewhere.
Those methods .deselect() and .select() are actually tkinter Radiobuttons() methods. If you're tutorial reverences tkinter. "from tkinter import *" will import those class's. If not i have no idea why its being taken out of context.
But what this code does is, it takes 'i', which is a button you click, then it sets 'state' to 'i', making 'state' 'global' over writing the last 'state', then it deselects all buttons, and selects 'i' which is your new choice. In effect this means you'll only ever select 1 button
If you wonder why state is an empty string, its because an empty string is the first button in tkinter. When you pass a new string in, it gives the new item being something like "I001" or something.
I have a pyside application with a numerous buttons, toolbuttons, tabs etc... I would like all of them to have a 'pointing hand' cursor when hovering/clicking. This means my code is full of statements like this:
someWidget.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
It is a relatively long, ugly line.
I can encapsulate the above in a function:
def hand_cursor(widget):
widget.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
This makes the rest of the code somewhat neater:
hand_cursor(someWidget)
But I still have to write that line all over the place.
Does anyone know of a recipe/trick to be able to get all my buttons, tab bars etc to have the pointing hand cursor without repeating myself all over?
I would maintain a list of all the widgets for which you want this behaviour, and loop:
widgetsToChange = [someWidget, anotherWidget, ...]
for w in widgetsToChange:
hand_cursor(w)
#or w.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
You can also use QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) to change for all the application.