# -*- coding: utf-8 -*-
r""" tkAskString.py uses TK to ask the user for a string & prints it out
Usage: ./tkAskString.py
Sample: ./tkAskString.py
see:
1. ["Example 2" - Python tkinter.simpledialog.askstring() Examples](https://www.programcreek.com/python/example/100669/tkinter.simpledialog.askstring)
2. [How do I get rid of Python Tkinter root window?](https://stackoverflow.com/questions/1406145/how-do-i-get-rid-of-python-tkinter-root-window)
3. [Tkinter Dialogs (Python3.10)](https://docs.python.org/3/library/dialog.html)
4. google: tkinter simpledialog set width | tkinter simpledialog keyword arguments
5. ["add extra tabs at the end of your prompt"](https://stackoverflow.com/a/69396488/601770)
"""
import tkinter as tk
from tkinter import simpledialog
# root = tk.Tk()
def main():
root = tk.Tk()
root.withdraw()
uname = simpledialog.askstring("My Title", "Enter username:\t\t\t") # Tabs make it wide as i want
print(uname)
return uname
if __name__ == "__main__":
main()
Tag Archives: Tkinter
Real World Tkinter GUI
Here is a real world Tkinter GUI that I made for a command line application that translates between MIDI files and Text files. The code follows the screen shot.
# -*- coding: ISO-8859-1 -*- import sys if not('.' in sys.path): sys.path.append('.') import midi24txt from Tkinter import * from tkFileDialog import * # thinking in tkinter http://www.ferg.org/thinking_in_tkinter/all_programs.html class TheGui: def __init__(self, parent): #------- frmSetup ----------# self.frmSetup = Frame(parent, bd=5) self.frmSetup.pack() self.inChoices = ('Text', 'Midi') self.varRadio = IntVar() self.r1 = Radiobutton(self.frmSetup, text="Convert Text INPUT into Midi OUTPUT", variable=self.varRadio, value=0, command=self.selRadio) self.r1.pack(anchor=W) self.r2 = Radiobutton(self.frmSetup, text="Convert Midi INPUT into Text OUTPUT", variable=self.varRadio, value=1, command=self.selRadio) self.r2.pack(anchor=W) #------- frmSetup ----------# sep = Frame(parent, width=1, bd=5, bg='black') sep.pack(fill=X, expand=1) #------- frmIn ----------# # http://effbot.org/tkinterbook/tkinter-widget-styling.htm self.frmIn = Frame(parent, bd=5) self.frmIn.pack() self.lblIn = Label(self.frmIn, text='Text Input File Path', width=20) self.lblIn.pack(side=LEFT) self.inFilePath = StringVar() # http://effbot.org/tkinterbook/entry.htm self.entIn = Entry(self.frmIn, width=20, textvariable=self.inFilePath) self.entIn.pack(side=LEFT) self.btnIn = Button(self.frmIn, text='Browse', command=self.btnInBrowseClick) self.btnIn.pack(side=LEFT) #------- frmIn ----------# #------- frmOut ----------# self.frmOut = Frame(parent, bd=5) self.frmOut.pack() self.lblOut = Label(self.frmOut, text='Midi Output File Path', width=20) self.lblOut.pack(side=LEFT) self.outFilePath = StringVar() self.entOut = Entry(self.frmOut, width=20, textvariable=self.outFilePath) self.entOut.pack(side=LEFT) self.btnOut = Button(self.frmOut, text='Browse', command=self.btnOutBrowseClick) self.btnOut.pack(side=LEFT) #------- frmOut ----------# sep = Frame(parent, width=1, bd=5, bg='black') sep.pack(fill=X, expand=1) #------- frmButtons ----------# self.frmOut = Frame(parent, bd=5) self.frmOut.pack() self.btnConvert = Button(self.frmOut, text='Convert', command=self.btnConvertClick) self.btnConvert.pack() #------- handle commands ----------# def selRadio(self): self.lblIn.config(text = self.inChoices[self.varRadio.get()] + ' Input File Path') self.lblOut.config(text = self.inChoices[(self.varRadio.get()+1)%2] + ' Output File Path') print str(self.varRadio.get()) def btnInBrowseClick(self): rFilepath = askopenfilename(defaultextension='*', initialdir='.', initialfile='', parent=self.frmIn, title='select a file') self.inFilePath.set(rFilepath) print self.entIn.get() def btnOutBrowseClick(self): rFilepath = asksaveasfilename(defaultextension='*', initialdir='.', initialfile='', parent=self.frmIn, title='select a file') self.outFilePath.set(rFilepath) print self.entOut.get() def btnConvertClick(self): if self.varRadio.get() == 0: inputTextFilePath = str(self.inFilePath.get()) outputMidiFilePath = str(self.outFilePath.get()) print 'midi 4 txt', inputTextFilePath, outputMidiFilePath midi24txt.mid4txt(inputTextFilePath, outputMidiFilePath) else: inputMidiFilePath = str(self.inFilePath.get()) outputTextFilePath = str(self.outFilePath.get()) print 'midi 2 txt', inputMidiFilePath, outputTextFilePath midi24txt.mid2txt(inputMidiFilePath, outputTextFilePath) root = Tk() root.title("Convert between Midi and Text Files") #http://infohost.nmt.edu/tcc/help/pubs/tkinter/std-attrs.html#geometry #http://infohost.nmt.edu/tcc/help/pubs/tkinter/toplevel.html root.geometry("350x200+10+10") gui = TheGui(root) root.mainloop()