So I did earlier, a few months ago, ask a similar question but I can't seem to apply the same rules/laws as were applied in a previous question of mine. Like my name suggest my understanding of Python is very basic and so I am confused at why his code won't work...
Every time I load a new screen, the old set of boxes should disappear and new only the new ones should be left there. However this is not what happens. Here are pictures to demonstrate:
Link to the full code.
However I think the problem may lie here in this part of the code:
def deletePanes(self):
print("delete panes")
Panes = []
self.NoOfPanes = 0
I also call Panes = [] in an earlier section of the code which lies in class Application():. Is the problem possibly that I am calling it twice? What I mean by this is that is it possible that one Panes list has the old boxes in it and one Panes has the new set in it and so when it comes to draw on the PyGame screen it draw both old and new boxes? (All of this might make more sense if you have a look at the full code)
Thank you for any help in advance.
When you call
Panes = []
inside a function, it creates a new local variable Panes and sets it to an empty list.
If it is a global variable, you should call
Panes[:] = []
If it is in the class, you should call
self.Panes = []
Panes is a local variable and will not change the value of your other use of Panes. You need to either make Panes global or an instance variable. Do the same in each place in your code that references Panes
Related
It appears that when a line of code like the following:
class PostStatsPage(Screen, MDAdaptiveWidget):
name1 = StringProperty(pg1.name)
name2 = StringProperty(sg1.name)
name3 = StringProperty(sf1.name)
name4 = StringProperty(pf1.name)
name5 = StringProperty(c1.name)
pts1 = StringProperty(str(pg1.stats))
pts2 = StringProperty(str(sg1.stats))
is written for a screen, the variables are set as soon as the app is started. My app lets the user set the variables in screens leading up to this screen, so when I set my labels to reference these StringProperties, they call the default values written into the code.
My question is: Is there a simple way to update a screen at once? I know that I could set the button that takes me to this screen to update every variable (essentially writing this code twice), but that doesn't seen very "pythonic," and I'm sure it isn't the best way to do it. That would be a nightmare in a large scale application.
I'd like to think there is a function like app.update(PostStatsPage) that would recall all of the objects inside of the StringProperties of the screen given as an argument. Is there anything like that? I've tried searching, and all I can find is articles about threading, but that looks like its used for running multiple code simultaneously. I don't believe I need that for this simple use case. I can't be the only person who finds this a necessity. Thanks in advance!
Colton
I have a bunch of screens in my app that all have the same icon that I always want to change together. So I currently have it hard coded and everytime I add a new screen I have to add a new line and it's getting cumbersome:
self.tcs_screen.ids.statusicon.source = "/imgs/..."
self.eclss_screen.ids.statusicon.source = "/imgs/..."
self.gnc_screen.ids.statusicon.source = "/imgs/..."
...
Is it possible to do this from a loop over a list of the screens? I've been trying the following with no success (how do you insert a variable into a property assignment?):
ScreenList = ['tcs_screen', 'eclss_screen', 'gnc_screen']
for x in xrange(len(ScreenList)):
self.ScreenList[x].ids.statusicon.source = "/imgs/..."
Or is there a better way to accomplish this?
Thanks
You have to use getattr() to get the property using the name.
screen_list = ['tcs_screen', 'eclss_screen', 'gnc_screen']
for e in screen_list:
getattr(self, e).ids.statusicon.source = "/imgs/..."
folks! So, thanks to you guys I was able to figure out what it was I was doing wrong in my previous script of staggering animation for selected objects in a scene. I am now on part two of this little exercise: Creating a UI for it.
This involves creating a window with a button and user input of how much the animation will be staggered by. So, instead of me putting how much the stagger should increment by (which was two in my previous script), I'd now allow the user to decide.
The script I have so far created the window, button, and input correctly, though I am having some trouble with getting the UI to properly execute, meaning when I click on the button, no error pops up; in fact, nothing happens at all to change the scene. I get the feeling it's due to my not having my increment variable in the correct spot, or not utilizing it the right way, but I'm not sure where/how exactly to address it. Any help would be greatly appreciated.
The code I have (with suggested edits) is as follows:
import maya.cmds as cmds
spheres = cmds.ls(selection=True)
stagWin = cmds.window(title="Stagger Tool", wh=(300,100))
cmds.columnLayout()
button = cmds.button(label="My Life For Aiur!")
count = cmds.floatFieldGrp(fieldgroup, query=True, value=True)
fieldgroup = cmds.floatFieldGrp(numberOfFields=1)
cmds.button(button, edit=True, command=lambda _:stagger(fieldgroup))
cmds.showWindow(stagWin)
def stagger(fieldgroup):
for i in spheres:
cmds.selectKey(i)
cmds.keyframe(edit=True, relative=True, timeChange=count)
print "BLAH"
Moving the comments into an answer because I think I've got it all figured out finally:
First of all, the better practice is to pass the stagger object to the button command rather than the string. so that would be:
cmds.button(label="My Life For Aiur!", command=stagger)
Secondly, the count isn't getting updated, so it stays 0 as per your third line. To update that:
count = cmds.floatFieldGrp(fieldgroup, query=True, value=True)
But wait, where did fieldgroup come from? We need to pass it into the function. So go back to your button code and take out the command entirely, also saving the object to a variable:
button = cmds.button(label="My Life For Aiur!")
Now store the object for the fieldgroup when you make it:
fieldgroup = cmds.floatFieldGrp(numberOfFields=1)
Now that you have fieldgroup, you can pass that in the function for the button, like this:
cmds.button(button, edit=True, command=lambda _:stagger(fieldgroup))
I had to wrap the function in a lambda because we're passing fieldgroup, but if I just put stagger(fieldgroup) it would call that and pass the result of that into the command for the button
Also update stagger def with fieldgroup argument:
def stagger(fieldgroup):
One final note that won't actually affect this, but good to know:
when you shift the keyframes inside stagger you're using a DIFFERENT count variable than the one you declared as 0 up above. The outer one is global, and the inner is local scope. Generally it's best to avoid global in the first place, which fortunately for you means just taking out count = 0
Putting that all together:
import maya.cmds as cmds
spheres = cmds.ls(selection=True)
stagWin = cmds.window(title="Stagger Tool", wh=(300,100))
cmds.columnLayout()
button = cmds.button(label="My Life For Aiur!")
fieldgroup = cmds.floatFieldGrp(numberOfFields=1)
cmds.button(button, edit=True, command=lambda _:stagger(fieldgroup))
cmds.showWindow(stagWin)
def stagger(fieldgroup):
count = 0
increment = cmds.floatFieldGrp(fieldgroup, query=True, value=True)[0]
print count
for i in spheres:
cmds.selectKey(i)
cmds.keyframe(edit=True, relative=True, timeChange=count)
count += increment
print "BLAH"
I'll admit, i am a newbie with python, but here is my issue.
the version is 2.6.5 (i know i'ts an old version but there's reasons to this) and livewires is used
Bascially this game has a bunch of colored balloons in which you need to click them to make them disappear. Adjacent balloons of the same color disappear along with the clicked balloon. Once the balloons are cleared it moves on to the next level.
I need to create a timer on the top right of my screen. This timer needs to countdown in seconds (from 30 might be a good start.) However no matter what i try, either the timer does not display or the numbers are overlap eachother. I would like to know how to do this, as it has been driving me up the wall as of late.
...Of course it also needs to end the game if it reaches zero and add more time if the level is complete...
But for now i just want to focus on displaying the timer and having it count down to zero on screen.
class Timer(games.Sprite):
""" countdown timer """
def __init__(self):
timer_message = games.Text(
value = 30,
size = 50,
color = red,
x = 600,
y = 30
)
def start(self):
while self.timer_message.value != 0:
time.sleep(1)
self.timer_message.value -= 1
game.screen.add(timer_message)
Alright. I fixed the "compressing balloons table" (accidentally deleted the self_update lol) problem, but now it is saying that "global name timer_message is not defined"... despite the fact that it says timer_message = games.Text
I would paste the whole code, but i can't get the indentation right (this is my first time using this website.)
So, I understand it's been some time and if you don't need an answer anymore that's alright.
For now it's hard to answer your question in general because I don't understand the structure of the rest of your code or how you're displaying graphics. However, I can tell you while you're getting the
global name timer_message is not defined
error. It's because when you define timer_message within the __init__ function you are defining it within the local scope of the function but not for the class. In order to make it accessible to the class you need to assign to self.timer_message.
This is a consequence of how python imitates object oriented programming, but making this change should address your immediate error.
I am trying to create a program in tkinter that allows people to rename a log file to whatever is typed into a text entry box. However this is not going to plan.
EDITED Thanks to Bryan Oakley.
I have slaved the rename function to a button however my new issue is that the values for contents are a weird set of numbers. These appear to be randomly generated every time I run the rename function.
These numbers look like
44499952get
44452520get
46401376get
46400496get
44688048get
44697440get
Can anyone please help or explain what these numbers mean?
Look at this code:
newname_ent = Entry(self,width = 50,)
contents = newname_ent.get()
It seems highly unlikely that the user will be able to type in something in the millisecond or so between creating the widget and getting the value.
You need to create a button or set an event binding that will call a function after the user has the chance to enter some information. That function is where you will put the code to do the rename.