Click here to Skip to main content
15,886,137 members
Articles / Programming Languages / Python
Tip/Trick

Alternative to "Command Window Here" Using Simple tkinter Script

Rate me:
Please Sign up or sign in to vote.
3.86/5 (5 votes)
31 May 2017CPOL1 min read 6.8K   3   1
Tkinter Command Window Here Launcher

Introduction

Here's a little python script that solves the number one problem of shell programming in a Windows environment, namely: How to open the command window in the correct folder?

Many people have clicked and mis-clicked over the years using the "shift-click folder" shortcut built into Microsoft explorer. Personally, I've always found this method a bit clumsy, mixing up the shift and control keys and not clicking on the right folder icon where the "command window here" menu is active.

Recently, I was playing around with python and tkinter on Windows and discovered a new trick for opening a command window wherever you want them. The Idea is super easy and almost trivial: The program just prompts the use for a directory to select and opens a customize windows command box in the selected directory.

Why solve this problem using python? Simply because that's what i was using at the time when i needed to open a command box in a selected directory. But, certainly I could imagine a C# freeware program that does a similar function, and perhaps expands on the idea by providing a launcher menu with adjustable parameters and loadable profiles to create your command box from. I'm just presenting the simplest lowest common denominator approach using Python.

Using the Code

The code is really simple. Popup a tkinter dialog box prompting the user to select a directory. Then, pass this information on to a system call to start a command box.

The only trick to this simple program, under tkinter environment, is to hide the toplevel window, so that it only shows the dialog box.

Here's the code:

Python
#!python3
"""
File    : m$term.pyw
Creator : WPM (Bill Moore)

tkinter gui to open windows command box
in a selected directory

SETUP: make sure pyw is associated with "pythonw.exe" from microsoft explorer 
       and place this script on your desktop so that it can be double-clicked
       whenever you need it.
"""

import tkinter as tk
import tkinter.filedialog
import sys
import os

def TkStartTerm():
    result = tk.filedialog.askdirectory(
        initialdir = ".",
        mustexist  = True,
        title = "Open Command Window Here"
    )
    if not result:
        return None

    os.chdir(result)

    os.system(
        'start cmd /k "' +
        'color 1f & ' +
        'mode con: cols=140 lines=15000 &' +
        'title cmd ' +
        '"'
    )

def TkRootHide():
    tk.Tk().withdraw()

##########################################
if __name__ == '__main__':

    TkRootHide()
    TkStartTerm()
    sys.exit(0)

That's it. Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionCan we control the cmd window size ? Pin
help2ray30-Sep-17 18:48
help2ray30-Sep-17 18:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.