Python Forum
python file(.py) not working with my tkinter project
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python file(.py) not working with my tkinter project
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():

    form = Tk()
    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):
            website = web.get()
            password = pas.get()
            if website not in new_webpas:
                fst_password = Label(tab2, text=str(website) + '==>' + str(password))
                fst_paste_button = Button(tab2, text="Copy", command=lambda: copy(str(password)))
                fst_del_button = Button(tab2, text="Delete")
                website_entry.delete(0, END)
                passwords_entry.delete(0, END)
                print("You already have this website in the list")
        except AttributeError:
            fst_password = Label(tab2, text=str(web) + '==>' + str(pas))
            fst_paste_button = Button(tab2, text="Copy", command=lambda: copy(str(pas)))
            fst_del_button = Button(tab2, text="Delete", command=lambda: delete(fst_del_button, fst_paste_button, fst_password, web, pas))

    def delete(delete, pst, password, website, pas):

    def copy(password):

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

    def updater():
        lstfile_r = open("C:\\Users\\Dean\\Desktop\\Passwords.txt", "r")
        readtext =
            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:
                    index += 1
                    print("You already have this password in the list")
                website = item
                if website not in new_webpas:
                    viewer(website, password)
                    index += 1
                    print("You already have this website in the list")

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

    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')

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

root = Tk()

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
  • 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

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,

Possibly Related Threads…
Thread Author Replies Views Last Post
  Using Tkinter inside function not working Ensaimadeta 3 608 Dec-23-2020, 02:57 PM
Last Post: deanhystad
  [Tkinter] Tkinter - I need to read file excel from GUI app to script file johnjh 0 4,599 Apr-17-2020, 08:14 PM
Last Post: johnjh
  Tkinter scaling windows conten to or with its size not working Detzi 5 1,392 Jan-12-2020, 12:42 PM
Last Post: Detzi
  [Tkinter] Mouse click event not working on multiple tkinter window evrydaywannabe 2 1,532 Dec-16-2019, 04:47 AM
Last Post: woooee
  Problem In calling a Function from another python file of project vinod2810 7 1,577 Oct-05-2019, 01:09 PM
Last Post: ichabod801
  [Tkinter] sleep(n) not working inside tkinter mainloop roger31415 2 2,491 Jul-14-2019, 06:57 PM
Last Post: wuf
  Python, Tkinter. How to search a specific file in a folder Roo 3 1,521 Feb-13-2019, 10:41 AM
Last Post: Larz60+
  tkinter - global variable not working albry 2 4,104 Jan-26-2019, 04:22 AM
Last Post: Larz60+
  [Tkinter] TkInter Setting Out Project MTom5 1 1,105 Aug-14-2018, 04:08 PM
Last Post: Larz60+
  Tkinter, button doesnt working ;// Dzonzi 3 6,096 Jul-18-2018, 11:14 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020