-1

I'm working on a new project in visual studio as shown in the code below, and the GUI using Tkinter is not working in visual studio. This is my first time using visual studio and I can't seem to find why it won't work.

from tkinter import *
import tkinter as ttk
#import os      #not needed
root = Tk()

#Setting up the window

root.geometry("250x100")
root.resizable(width=False, height=False)#Disables user to resize window
root.title("Login")

#Temp "DataBase"

users=[("josh","python")] #<<<here ''josh'' is user and ''python'' i5s         password
admins=[("josh1","python1")]

# Login and signup function

def login():        #login function

    if (t1.get(),t2.get())in users:     #Temp for testing
        root.destroy()
        import MainWindow
#        os.system("MainWindow")        #does not work
        print("welcome")

    elif (t1.get(),t2.get())in admins:      #Temp for testing
        root.destroy()
        import AdminMainWindow
#        os.system("AdminMainWindow")     #does not work
        print("welcome Admin")

    else:

        error.config(text="Invalid username or password")

def signup():       #signup function
    root.destroy
    import SignupWindow
#    os.system("SignupWindow")     #does not work

#arranging display varables

top = Frame(root)
bottom = Frame(root)
top.pack(side=TOP, fill=BOTH, expand=True)
bottom.pack(side=BOTTOM, fill=BOTH, expand=True)

#error placement and font

error = Label(root, font=("blod",10))
error.place(x=40,y=55)

#input display setup

l1 = Label(root,text="Username:")
l2 = Label(root,text="Password:")

t1 = Entry(root, textvariable=StringVar())
t2 = Entry(root, show="*", textvariable=StringVar())

b1 = Button(root,text="Login", command=login)
b2 = Button(root,text="Signup", command=signup)

#organising

l1.pack(in_=top, side=LEFT)
t1.pack(in_=top, side=LEFT)
l2.pack(side=LEFT,)
t2.pack(side=LEFT,)
b1.pack(in_=top, side=BOTTOM)
b2.pack(in_=bottom, side=BOTTOM)

#end of Tk loop

root.mainloop()

It comes up with the python command line and says press any key to continue.

I also looked online and they all say it because people don't end the Tk loop, but I have.

visual studio screenshot

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
josh_meh
  • 11
  • 1
  • 2
  • add `print()` to see if it will print anything. I see you have more that one file in project - maybe it starts different file from your project. Put some `print()` in every file to see which file is executed. – furas Jan 28 '17 at 21:04
  • 1
    Since your code works outside of VS, it seems likely that it has something to do with how VS runs Python scripts. Do as @furas suggests and put a `print('starting loop')` right before the `root`,mainloop()` call and see if it gets that far. The most likely problem, since you are seeing the Python console, is that the script's filename isn't being passed to the interpreter. – martineau Jan 28 '17 at 21:07
  • Thanks, guys, but it works perfectly outside of VS and iv added some prints outside of the loop and it does not print them in the command window or the command line. However, if I create a new project and just have print("Hi") it comes up with the same thing but prints "Hi" as an str in the command line like a asked it to do. @furas – josh_meh Jan 28 '17 at 21:21
  • 1
    did you add `print()` in all your files ? Project may have assigned one file which it execute always when you run project and it can be different file than you expect. – furas Jan 28 '17 at 21:27
  • Or simple it is some bug in VS so start new project, create new files in this project and copy-paste code from old project. – furas Jan 28 '17 at 21:28
  • done both and it still don't work @furas – josh_meh Jan 29 '17 at 08:36
  • FOUND IT! before you make a new project i created a new file and places all the code in there. then add one code to VS at a time then it works but not when you do it all together. @furas – josh_meh Jan 29 '17 at 08:43
  • I was having the same problem, with a simple "Hello World". I ended up adding print('starting loop') and it then printed that text and showed the Tk window. Then I removed print('starting loop') and now the program works correctly. VS is a strange beast sometimes – Guy Starbuck Feb 21 '19 at 21:41

2 Answers2

0

before you make a new project I created a new file and places all the code in there. then add one code to VS at a time then it works but not when you do it all together.

josh_meh
  • 11
  • 1
  • 2
0

On ms-windows, python programs using tkinter should have the extension .pyw. And this extension should be associated with pythonw.exe rather than python.exe.

Using pythonw.exe will prevent the cmd.exe window from appearing when your python script has a GUI.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94