0

I am a beginner in python and I would like to try to create an application involving Tkinter.

I found the VisualStudio Code application writing environment, which seems to be a pretty good application.

I noticed that it executes any script with python.exe. which has the effect of not executing the scripts involving tkinter.

I understood that it is necessary to create an environment for the development of Tkinter applications within Vs Code, an aspect about which I do not know much.

I would like to ask if you could provide me with some information or ways in which I can configure Vscode to be able to run the codes involving tkinter.

I tried to find some information on the internet about the vscode configuration but I didn't find anything relevant. An example code could be:

from tkinter import *

def main():
    root = Tk ()
    application  = Window1 (root)
    
class Window1:
      def __init__(self,master):
          self.master = master
          self.master.title('Sistem de Logare')
          self.master.geometry('600x800')
          self.frame = Frame(self.master)
          self.frame.pack()
          self.label = Label(self.frame, text = 'Hello')
          self.label.grid() 
          self.btn = Button (self.frame , text= ' Open Window 2' , command = self.window2) 
          self.btn.grid()

      def window2 (self):
             self.window2 = Toplevel (self.master)
             self.application = window2(self.window2)

class window2:
    def __init__ (self,master):
        self.name= StringVar()
        self.master=master
        self.master.title('Window2')
        self.master.geometry('1350x750')
        self.framew2 = Frame(self.master)
        self.framew2.pack()
        self.name = Entry(self.framew2, textvariable = self.name)
        self.name.grid()
        self.btn = Button (self.framew2 , text= ' Open Window 2' , command = '') 
        self.btn.grid()
  
         
if __name__ == '__main__':
    main()
valy
  • 41
  • 6
  • maybe run the script "manually" in the prompt/shell: `py .py` – Edoardo Balducci Apr 05 '23 at 08:58
  • Why do you think `python.exe` would not work with `tkinter`? How would you run it instead? Do you have an example of code that would typically run but doesn't with VS Code? – Axe319 Apr 05 '23 at 08:59
  • Because if I try to run a code that involves tkinter, it doesn't run. If I try to run a code that does not involve tkinter, it is run correctly. Maybe it's the wrong configuration in terms of using tkinter in VScode Axe319 – valy Apr 05 '23 at 09:06
  • Right, but have you run the same code from command line? What do you mean by "doesn't run"? Do you get an error? If so, please post the error. The reason I ask is because it should make no difference whether you use VS Code or run it any other way, if you're using the same interpreter, it will still work. – Axe319 Apr 05 '23 at 09:17
  • Running the code from the command line doesn't make any difference. To be able to run a tkinter script the way I found it efficient is to use IDLE python located in "C:\Users\dell\AppData\Local\Programs\Python\Python311 -32\Lib\idlelib\idle.pyw”. Running through VS code does not involve the use of IDLE, as we noticed, but directly python exe, the result being the non-display of the widgets involved in the script that contain TKinter elements. – valy Apr 05 '23 at 10:05
  • Please include the script your attempting to run or a [MRE]. Most of the assumptions you're making are simply not true or based on false assumptions. We can't tell you why it's not working if you don't show it to us. – Axe319 Apr 05 '23 at 10:37
  • from tkinter import * def main(): root = Tk () application = Window1 (root) class Window1: def __init__(self,master): self.master = master self.master.title('Sistem de Logare') self.master.geometry('600x800') self.frame = Frame(self.master) self.frame.pack() self.label = Label(self.frame, text = 'x') self.label.grid() self.btn = Button (self.frame , text= ' Open Window 2' , command = self.window2) self.btn.grid() – valy Apr 05 '23 at 11:01
  • This could be an example of code that doesn't work. – valy Apr 05 '23 at 11:04
  • Please [edit] your post and include it there. The example in the comment doesn't call your `main` function and references a `self.window2` which doesn't exist. – Axe319 Apr 05 '23 at 11:26
  • @valy You never call `application.mainloop()` in your `main` function. The difference is, if you're using IDLE or any other interactive window, you don't need to call `mainloop`. As a script, you do. – Axe319 Apr 05 '23 at 11:48
  • Axe319 thanks for the reply. This solved the problem. If I'm not too insistent, I could ask how to display in the label from window 1 what I enter in window2 in the case of the current code, taking into account that the variable that stores the label is an initialization specific to the window1 class only. Thank you anticipated – valy Apr 05 '23 at 12:43

0 Answers0