1

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?

2
  • 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, 2012 at 20:09
  • To make example more simple I put public :D
    – user26030
    Jul 3, 2012 at 21:28

2 Answers 2

1

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.

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

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)

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .