How to change value of a variable of a python file from another file? The 2019 Stack Overflow Developer Survey Results Are InHow do I check whether a file exists without exceptions?How do I copy a file in Python?How can I safely create a nested directory in Python?How to get the current time in PythonHow can I make a time delay in Python?How do I sort a dictionary by value?How do I pass a variable by reference?Why can't Python parse this JSON data?How do I list all files of a directory?How to access environment variable values?

Idiomatic way to prevent slicing?

Inflated grade on resume at previous job, might former employer tell new employer?

Landlord wants to switch my lease to a "Land contract" to "get back at the city"

How can I create a character who can assume the widest possible range of creature sizes?

Manuscript was "unsubmitted" because the manuscript was deposited in Arxiv Preprints

Is there a name of the flying bionic bird?

What are the motivations for publishing new editions of an existing textbook, beyond new discoveries in a field?

Can distinct morphisms between curves induce the same morphism on singular cohomology?

How to manage monthly salary

Is bread bad for ducks?

Access elements in std::string where positon of string is greater than its size

How long do I have to send payment?

The difference between dialogue marks

How to change the limits of integration

Realistic Alternatives to Dust: What Else Could Feed a Plankton Bloom?

How come people say “Would of”?

Unbreakable Formation vs. Cry of the Carnarium

Should I use my personal or workplace e-mail when registering to external websites for work purpose?

Are USB sockets on wall outlets live all the time, even when the switch is off?

Does light intensity oscillate really fast since it is a wave?

"What time...?" or "At what time...?" - what is more grammatically correct?

I see my dog run

Inline version of a function returns different value then non-inline version

Where does the "burst of radiance" from Holy Weapon originate?



How to change value of a variable of a python file from another file?



The 2019 Stack Overflow Developer Survey Results Are InHow do I check whether a file exists without exceptions?How do I copy a file in Python?How can I safely create a nested directory in Python?How to get the current time in PythonHow can I make a time delay in Python?How do I sort a dictionary by value?How do I pass a variable by reference?Why can't Python parse this JSON data?How do I list all files of a directory?How to access environment variable values?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have two python file. I want to change variable value by calling the module.



test.py



import tkinter
from tkinter import *
from tkinter import ttk

class Master(Tk):
def __init__(self,parent):
Tk.__init__(self,parent)
self.initialize()


def initialize(self):
self.grid()
self.my_clock = StringVar(self)
self.my_clock.set("12:05")

clock =Label(self,textvariable = self.my_clock,height=1,
width=5,fg="black",bg="white",font=("Sans", 32,"bold"))

clock.grid(column=0,row=0,sticky='SW')




window = Master(None)
window.configure(background="white")
window.mainloop()


test1.py



import test
test.window.my_clock.set("13:05")


I could not change the value of my_clock variable from test1.py file.How could I change the value of my_clock variable from test1.py file.










share|improve this question
























  • Why do you pass a parent argument to your class? What do you think you can pass here? A Tk instance does not have a parent.

    – fhdrsdg
    Mar 8 at 12:47

















0















I have two python file. I want to change variable value by calling the module.



test.py



import tkinter
from tkinter import *
from tkinter import ttk

class Master(Tk):
def __init__(self,parent):
Tk.__init__(self,parent)
self.initialize()


def initialize(self):
self.grid()
self.my_clock = StringVar(self)
self.my_clock.set("12:05")

clock =Label(self,textvariable = self.my_clock,height=1,
width=5,fg="black",bg="white",font=("Sans", 32,"bold"))

clock.grid(column=0,row=0,sticky='SW')




window = Master(None)
window.configure(background="white")
window.mainloop()


test1.py



import test
test.window.my_clock.set("13:05")


I could not change the value of my_clock variable from test1.py file.How could I change the value of my_clock variable from test1.py file.










share|improve this question
























  • Why do you pass a parent argument to your class? What do you think you can pass here? A Tk instance does not have a parent.

    – fhdrsdg
    Mar 8 at 12:47













0












0








0








I have two python file. I want to change variable value by calling the module.



test.py



import tkinter
from tkinter import *
from tkinter import ttk

class Master(Tk):
def __init__(self,parent):
Tk.__init__(self,parent)
self.initialize()


def initialize(self):
self.grid()
self.my_clock = StringVar(self)
self.my_clock.set("12:05")

clock =Label(self,textvariable = self.my_clock,height=1,
width=5,fg="black",bg="white",font=("Sans", 32,"bold"))

clock.grid(column=0,row=0,sticky='SW')




window = Master(None)
window.configure(background="white")
window.mainloop()


test1.py



import test
test.window.my_clock.set("13:05")


I could not change the value of my_clock variable from test1.py file.How could I change the value of my_clock variable from test1.py file.










share|improve this question
















I have two python file. I want to change variable value by calling the module.



test.py



import tkinter
from tkinter import *
from tkinter import ttk

class Master(Tk):
def __init__(self,parent):
Tk.__init__(self,parent)
self.initialize()


def initialize(self):
self.grid()
self.my_clock = StringVar(self)
self.my_clock.set("12:05")

clock =Label(self,textvariable = self.my_clock,height=1,
width=5,fg="black",bg="white",font=("Sans", 32,"bold"))

clock.grid(column=0,row=0,sticky='SW')




window = Master(None)
window.configure(background="white")
window.mainloop()


test1.py



import test
test.window.my_clock.set("13:05")


I could not change the value of my_clock variable from test1.py file.How could I change the value of my_clock variable from test1.py file.







python python-2.7 tkinter raspberry-pi3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 8 at 9:26







Akash Nil

















asked Mar 8 at 8:33









Akash NilAkash Nil

363416




363416












  • Why do you pass a parent argument to your class? What do you think you can pass here? A Tk instance does not have a parent.

    – fhdrsdg
    Mar 8 at 12:47

















  • Why do you pass a parent argument to your class? What do you think you can pass here? A Tk instance does not have a parent.

    – fhdrsdg
    Mar 8 at 12:47
















Why do you pass a parent argument to your class? What do you think you can pass here? A Tk instance does not have a parent.

– fhdrsdg
Mar 8 at 12:47





Why do you pass a parent argument to your class? What do you think you can pass here? A Tk instance does not have a parent.

– fhdrsdg
Mar 8 at 12:47












3 Answers
3






active

oldest

votes


















0














If you want your program test1 to show the window, then it needs to call window.mainloop().



import test
window = test.Master(None)
window.my_clock.set("13:05")
window.mainloop()





share|improve this answer






























    0














    You only call the mainloop when you run test.py directly (when __name__ == "__main__"). When you import it you need to run the mainloop again. Change test1.py to:



    import test

    m = test.Master(None)
    m.my_clock.set("13:05")

    m.mainloop()





    share|improve this answer






























      0














      When you do:



      import test
      test.Master(None).my_clock.set("13:05")


      You do the following:



      • Import test

      • Instanciate a new instance of the class Master (<=> a new object)

      • Set a new value for the property my_clock of your new object

      • Then this new object "disappear", as it was not stored into a variable.

      Two possibilities here:



      Instanciate your object in your test.py



      test.py



      class Master(Tk):
      def __init__(self,parent=None):
      (...)

      window = Master()
      (...)


      test2.py



      import test
      test.window.my_clock.set("13:05")


      Or use a class variable



      test.py



      class Master(Tk):
      my_clock = StringVar()
      def __init__(self,parent=None):
      (...)


      test2.py



      import test
      test.Master.my_clock.set("13:05")





      share|improve this answer

























      • Method1 shows the window with previous value. value not updated . and second method shows an error 'NoneType' object has no attribute '_root'

        – Akash Nil
        Mar 8 at 8:53











      • Those are only explanations about why your code could not work, and ideas to solve your problem, not a fully functionnal solution...

        – olinox14
        Mar 8 at 9:03











      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55059393%2fhow-to-change-value-of-a-variable-of-a-python-file-from-another-file%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      If you want your program test1 to show the window, then it needs to call window.mainloop().



      import test
      window = test.Master(None)
      window.my_clock.set("13:05")
      window.mainloop()





      share|improve this answer



























        0














        If you want your program test1 to show the window, then it needs to call window.mainloop().



        import test
        window = test.Master(None)
        window.my_clock.set("13:05")
        window.mainloop()





        share|improve this answer

























          0












          0








          0







          If you want your program test1 to show the window, then it needs to call window.mainloop().



          import test
          window = test.Master(None)
          window.my_clock.set("13:05")
          window.mainloop()





          share|improve this answer













          If you want your program test1 to show the window, then it needs to call window.mainloop().



          import test
          window = test.Master(None)
          window.my_clock.set("13:05")
          window.mainloop()






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 8 at 8:39









          BoarGulesBoarGules

          8,79721228




          8,79721228























              0














              You only call the mainloop when you run test.py directly (when __name__ == "__main__"). When you import it you need to run the mainloop again. Change test1.py to:



              import test

              m = test.Master(None)
              m.my_clock.set("13:05")

              m.mainloop()





              share|improve this answer



























                0














                You only call the mainloop when you run test.py directly (when __name__ == "__main__"). When you import it you need to run the mainloop again. Change test1.py to:



                import test

                m = test.Master(None)
                m.my_clock.set("13:05")

                m.mainloop()





                share|improve this answer

























                  0












                  0








                  0







                  You only call the mainloop when you run test.py directly (when __name__ == "__main__"). When you import it you need to run the mainloop again. Change test1.py to:



                  import test

                  m = test.Master(None)
                  m.my_clock.set("13:05")

                  m.mainloop()





                  share|improve this answer













                  You only call the mainloop when you run test.py directly (when __name__ == "__main__"). When you import it you need to run the mainloop again. Change test1.py to:



                  import test

                  m = test.Master(None)
                  m.my_clock.set("13:05")

                  m.mainloop()






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 8 at 8:39









                  fhdrsdgfhdrsdg

                  7,38322038




                  7,38322038





















                      0














                      When you do:



                      import test
                      test.Master(None).my_clock.set("13:05")


                      You do the following:



                      • Import test

                      • Instanciate a new instance of the class Master (<=> a new object)

                      • Set a new value for the property my_clock of your new object

                      • Then this new object "disappear", as it was not stored into a variable.

                      Two possibilities here:



                      Instanciate your object in your test.py



                      test.py



                      class Master(Tk):
                      def __init__(self,parent=None):
                      (...)

                      window = Master()
                      (...)


                      test2.py



                      import test
                      test.window.my_clock.set("13:05")


                      Or use a class variable



                      test.py



                      class Master(Tk):
                      my_clock = StringVar()
                      def __init__(self,parent=None):
                      (...)


                      test2.py



                      import test
                      test.Master.my_clock.set("13:05")





                      share|improve this answer

























                      • Method1 shows the window with previous value. value not updated . and second method shows an error 'NoneType' object has no attribute '_root'

                        – Akash Nil
                        Mar 8 at 8:53











                      • Those are only explanations about why your code could not work, and ideas to solve your problem, not a fully functionnal solution...

                        – olinox14
                        Mar 8 at 9:03















                      0














                      When you do:



                      import test
                      test.Master(None).my_clock.set("13:05")


                      You do the following:



                      • Import test

                      • Instanciate a new instance of the class Master (<=> a new object)

                      • Set a new value for the property my_clock of your new object

                      • Then this new object "disappear", as it was not stored into a variable.

                      Two possibilities here:



                      Instanciate your object in your test.py



                      test.py



                      class Master(Tk):
                      def __init__(self,parent=None):
                      (...)

                      window = Master()
                      (...)


                      test2.py



                      import test
                      test.window.my_clock.set("13:05")


                      Or use a class variable



                      test.py



                      class Master(Tk):
                      my_clock = StringVar()
                      def __init__(self,parent=None):
                      (...)


                      test2.py



                      import test
                      test.Master.my_clock.set("13:05")





                      share|improve this answer

























                      • Method1 shows the window with previous value. value not updated . and second method shows an error 'NoneType' object has no attribute '_root'

                        – Akash Nil
                        Mar 8 at 8:53











                      • Those are only explanations about why your code could not work, and ideas to solve your problem, not a fully functionnal solution...

                        – olinox14
                        Mar 8 at 9:03













                      0












                      0








                      0







                      When you do:



                      import test
                      test.Master(None).my_clock.set("13:05")


                      You do the following:



                      • Import test

                      • Instanciate a new instance of the class Master (<=> a new object)

                      • Set a new value for the property my_clock of your new object

                      • Then this new object "disappear", as it was not stored into a variable.

                      Two possibilities here:



                      Instanciate your object in your test.py



                      test.py



                      class Master(Tk):
                      def __init__(self,parent=None):
                      (...)

                      window = Master()
                      (...)


                      test2.py



                      import test
                      test.window.my_clock.set("13:05")


                      Or use a class variable



                      test.py



                      class Master(Tk):
                      my_clock = StringVar()
                      def __init__(self,parent=None):
                      (...)


                      test2.py



                      import test
                      test.Master.my_clock.set("13:05")





                      share|improve this answer















                      When you do:



                      import test
                      test.Master(None).my_clock.set("13:05")


                      You do the following:



                      • Import test

                      • Instanciate a new instance of the class Master (<=> a new object)

                      • Set a new value for the property my_clock of your new object

                      • Then this new object "disappear", as it was not stored into a variable.

                      Two possibilities here:



                      Instanciate your object in your test.py



                      test.py



                      class Master(Tk):
                      def __init__(self,parent=None):
                      (...)

                      window = Master()
                      (...)


                      test2.py



                      import test
                      test.window.my_clock.set("13:05")


                      Or use a class variable



                      test.py



                      class Master(Tk):
                      my_clock = StringVar()
                      def __init__(self,parent=None):
                      (...)


                      test2.py



                      import test
                      test.Master.my_clock.set("13:05")






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Mar 8 at 9:02

























                      answered Mar 8 at 8:38









                      olinox14olinox14

                      1,260618




                      1,260618












                      • Method1 shows the window with previous value. value not updated . and second method shows an error 'NoneType' object has no attribute '_root'

                        – Akash Nil
                        Mar 8 at 8:53











                      • Those are only explanations about why your code could not work, and ideas to solve your problem, not a fully functionnal solution...

                        – olinox14
                        Mar 8 at 9:03

















                      • Method1 shows the window with previous value. value not updated . and second method shows an error 'NoneType' object has no attribute '_root'

                        – Akash Nil
                        Mar 8 at 8:53











                      • Those are only explanations about why your code could not work, and ideas to solve your problem, not a fully functionnal solution...

                        – olinox14
                        Mar 8 at 9:03
















                      Method1 shows the window with previous value. value not updated . and second method shows an error 'NoneType' object has no attribute '_root'

                      – Akash Nil
                      Mar 8 at 8:53





                      Method1 shows the window with previous value. value not updated . and second method shows an error 'NoneType' object has no attribute '_root'

                      – Akash Nil
                      Mar 8 at 8:53













                      Those are only explanations about why your code could not work, and ideas to solve your problem, not a fully functionnal solution...

                      – olinox14
                      Mar 8 at 9:03





                      Those are only explanations about why your code could not work, and ideas to solve your problem, not a fully functionnal solution...

                      – olinox14
                      Mar 8 at 9:03

















                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55059393%2fhow-to-change-value-of-a-variable-of-a-python-file-from-another-file%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

                      Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

                      Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved