Python Forum

Full Version: python file(.py) not working with my tkinter project
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey everyone,
I'm trying to run my project by opening the .py project file and it doesn't work for some reason.
If I'm trying to run the code in Pycharm it works perfectly fine
but when I'm trying to open it from the .py file it start(you see the 'CMD' (black window) pop up and then close, the Tkinter window dose not show at all)
let me just say that I'm using the root.mainloop() function of course(in Pycharm it works fine) and my other Tkinter projects the .py file works fine too.
what can cause this kind of problem?
thanks Tongue
It would be helpful to know more information
This is my code:
from tkinter import *
from tkinter import ttk
import pyperclip
import ast


def fst_window():
    password_label = Label(root, text="Password:")
    password_label.grid(row=0, column=0)
    password_holder = StringVar()
    password_entry = Entry(root, textvariable=password_holder, show="*")
    password_entry.grid(row=0, column=1)
    enter1_button = Button(root, text="Enter", command=lambda: pass_checker(password_holder))
    enter1_button.grid(row=0, column=3)
    root.bind("<Return>", lambda event: pass_checker(password_holder))


def start_prog():
    root.destroy()

    form = Tk()
    form.title("Passwords")
    form.geometry("275x300+700+400")
    tab_parent = ttk.Notebook(form)

    tab1 = ttk.Frame(tab_parent)
    website_label = Label(tab1, text="Website:")
    website_label.grid(row=0, column=0)
    website_holder = StringVar()
    website_entry = Entry(tab1, textvariable=website_holder)
    website_entry.grid(row=0, column=1)
    passwords_label = Label(tab1, text="Password:")
    passwords_label.grid(row=1, column=0)
    passwords_holder = StringVar()
    passwords_entry = Entry(tab1, textvariable=passwords_holder, show="*")
    passwords_entry.grid(row=1, column=1)
    enter2_button = Button(tab1, text="Enter", command=lambda: viewer(website_holder, passwords_holder))
    enter2_button.grid(row=2, columnspan=2)
    form.bind("<Return>", lambda event: viewer(website_holder, passwords_holder))

    tab2 = ttk.Frame(tab_parent)
    new_webpas = []

    def viewer(web, pas):
        try:
            website = web.get()
            password = pas.get()
            if website not in new_webpas:
                new_webpas.append(password)
                new_webpas.append(website)
                fst_password = Label(tab2, text=str(website) + '==>' + str(password))
                fst_password.grid(column=0)
                fst_paste_button = Button(tab2, text="Copy", command=lambda: copy(str(password)))
                fst_paste_button.grid(column=1)
                fst_del_button = Button(tab2, text="Delete")
                fst_del_button.grid(column=2)
                website_entry.delete(0, END)
                passwords_entry.delete(0, END)
            else:
                print("You already have this website in the list")
        except AttributeError:
            fst_password = Label(tab2, text=str(web) + '==>' + str(pas))
            fst_password.grid(column=0)
            fst_paste_button = Button(tab2, text="Copy", command=lambda: copy(str(pas)))
            fst_paste_button.grid(column=1)
            fst_del_button = Button(tab2, text="Delete", command=lambda: delete(fst_del_button, fst_paste_button, fst_password, web, pas))
            fst_del_button.grid(column=2)

    def delete(delete, pst, password, website, pas):
        delete.grid_remove()
        pst.grid_remove()
        password.grid_remove()
        new_webpas.remove(pas)
        new_webpas.remove(website)

    def copy(password):
        pyperclip.copy(password)

    def saver():
        lstfile_wr = open("C:\\Users\\Dean\\Desktop\\Passwords.txt", "w+")
        lstfile_wr.write(str(new_webpas))
        lstfile_wr.close()

    def updater():
        lstfile_r = open("C:\\Users\\Dean\\Desktop\\Passwords.txt", "r")
        readtext = lstfile_r.read()
        lstfile_r.close()
        try:
            nano = ast.literal_eval(readtext)
        except SyntaxError:
            nano = ""
            print("Your file is empty")

        index = 1
        for item in nano:
            if index % 2 != 0:
                password = item
                if password not in new_webpas:
                    new_webpas.append(password)
                    index += 1
                else:
                    print("You already have this password in the list")
            else:
                website = item
                if website not in new_webpas:
                    new_webpas.append(website)
                    viewer(website, password)
                    index += 1
                else:
                    print("You already have this website in the list")

    tab3 = ttk.Frame(tab_parent)
    save_button = Button(tab3, text="Save", command=saver)
    save_button.pack()
    load_button = Button(tab3, text="Load", command=updater)
    load_button.pack()

    tab_parent.add(tab1, text="Enter Passwords")
    tab_parent.add(tab2, text="Copy Passwords")
    tab_parent.add(tab3, text="Load and Save")
    tab_parent.pack(expand=0, fill='both')
    updater()
    form.mainloop()


def pass_checker(gvn_password):
    cur_pass = "44"
    password = gvn_password.get()
    if password == cur_pass:
        start_prog()
    else:
        print("Password Incorrect!")


root = Tk()
root.title("Passwords")
root.geometry('225x30+750+480')
fst_window()

root.mainloop()
Sorry it's so messy but I'm new to coding.

My problem is that when I run the project in Pycharm it works fine, but when I'm trying to run it by opening the .py file it start normally and close before the Tkinter window even appeared.
I more on the lines meant these
Quote:
  • Describe the symptoms of your problem carefully and clearly.
  • Describe the environment in which it occurs. Provide the Python version, execution process, Operating System, 3rd party library, program structure, etc.
  • Describe the research you did to try and understand the problem before you asked the question.
  • Describe the diagnostic steps you took to try and pin down the problem yourself before you asked the question.
  • Describe any possibly relevant recent changes in your computer or software configuration. How did you install X program? From where? Provide the install link. Describe how you installed it, etc.
  • If at all possible, provide a way to reproduce the problem in a controlled environment.
  • Describe the goal, not the step. If you are trying to find out how to do something, begin by describing the goal. Only then describe the particular step towards it that you are blocked on.
  • Describe your problem's symptoms in chronological order
  • Describe the problem's symptoms, not your guesses
  • Write in clear, grammatical, correctly-spelled language
  • Use meaningful, specific thread/subject titles. From the title alone it should give a good indication of what the problem is. It should be a summary of your post. For the Smart Post above, you could use "I am getting an IndexError" or " Unexpected IndexError". If you are not getting the output expected and not getting an error at all, then it would be "Unexpected Output".
  • Separate the main question from the details to easily identify. Include a main question and do not assume the problem is obvious without it.
  • When I'm trying to run the code in Pycharm it works fine, but when I'm trying to run it by opening the .py file, a black window open and close.
  • Python 3.7 64bit, Pycharm 2019.2(community edition), windows 10 64bit, tkinter
  • I've searched online but didn't find anything.
  • Question: Why I can't run my program threw the project file (.py)?
Try the following code in a separate .py file.
import sys

print(sys.version)
input('press enter to continue')
run it in the same way and see what output you get in the black window.
Quote:3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]
press enter to continue

This is what got written in the black window.

This program is the only program I wrote that doesn't work this way, so I don't think that it's a problem with the python or somthing.
As that is working and seems to be the same version of python, the input stops the cmd window closing until you press enter.
Add input('1: press enter to continue') at various places in the tkinter code, changing the number each time.
Hopefully this will keep the cmd window open to see an error that's happened and pinpoint where it is not working.
I've tried to add the input function and it still doesn't show anything on the cmd window and then closes automatically.
(Jul-30-2019, 12:55 PM)DeanAseraf1 Wrote: [ -> ]I've tried to add the input function and it still doesn't show anything on the cmd window and then closes automatically.

I'm a newbie as well, same problem.
I pip installed all of my modules, it seems working now. :)

In your case:

pip install pyperclip
pip install AST

Best regards,
ifigazsi