Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have 1 main window and 1 dialog. Now I need to access variable in main window from dialog.

For example in java we have 2 classes: demo and myclass In my class I have following code: public static int number=5; Then I simply access it using myclass.number.

How to do it in python using quickly ide?

Do I need to import main window in dialog to get access to static variable?

share|improve this question
1  
I have no idea what quickly is but first of all you wouldn't normally declare a variable public in Java but private (or protected). Second I think that in python you would do import myclass in your other class, then you would instantiate it m=myclass() and should be able to access it m.number. You can also acccess it without instantiation. See stackoverflow.com/questions/68645/… for further information. –  onse Jul 3 '12 at 20:09
    
To make example more simple I put public :D –  user26030 Jul 3 '12 at 21:28
add comment

2 Answers

Just pass around your mainwindow instance, something like this:

class MyWindow(Gtk.Window):
    def __init__(self):
        # Do your thing...

        self.spam = "spam'n'eggs"

    def on_opendialog_clicked(self, widget):
        dialog = MyDialog(self)
        dialog.run()
        dialog.destroy()

class MyDialog(Gtk.Dialog):
    def __init__(self, mainwindow):
        # ...

        print(mainwindow.spam)

Have a look at my answer in this question too which is almost similar, only the other way around.

share|improve this answer
    
Error is on line: dialog = MyDialog(self) –  user26030 Jul 3 '12 at 22:46
    
Here you can see the code generated by quickly which probably causes error: pastebin.com/68fNcMdD –  user26030 Jul 3 '12 at 22:51
    
Please provide an actual traceback. "Probably" and "error" aren't of any help. –  Timo Jul 4 '12 at 7:41
    
TypeError: Must be string, not SampleWindow Here you can find sample application: ubuntuone.com/5eCaimCkGxOJn1SxUTTh5T –  user26030 Jul 4 '12 at 16:56
add comment

I'm not sure if this is the "pro" way to do it or not, but you can pass it to your variable when you generate it.

I have a file generation dialog I built by giving it the default output dir and default name of file. My code looks like:def __init__(self,parent,output_dir,filename), and when you call it make sure to pass it what it needs, eg. window(output_dir,filename)

share|improve this answer
    
Here is my code:ubuntuone.com/5eCaimCkGxOJn1SxUTTh5T I dont have init –  user26030 Jul 4 '12 at 16:57
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.