1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| #!/usr/bin/env python
#coding=utf-8
"""
Here we show how to have an easy-to-use graphical input.
For example, if you want to ask three variables to the user,
you just have to type something like :
a, b, c = ask("a =", "b =", "c =")
"""
try:
import Tkinter as tkinter
except:
import tkinter
answers = []
class ask_tk(tkinter.Tk):
def __init__(self, theTexts):
tkinter.Tk.__init__(self, None)
self.theText = theTexts
self.answers = []
self.labelVariable = []
self.entryVariable = []
self.entry = []
self.__initialize()
def __initialize(self):
self.grid()
for i, oneText in enumerate(self.theText):
# Static text
self.labelVariable.append(tkinter.StringVar())
currentLabelVariable = self.labelVariable[i]
label = tkinter.Label(self,
textvariable = currentLabelVariable)
label.grid(row=i,
column=0,
sticky=tkinter.E)
currentLabelVariable.set(oneText)
# Entry here the user can give its value
self.entryVariable.append(tkinter.StringVar())
currentEntryVariable = self.entryVariable[i]
self.entry.append(tkinter.Entry(self,
textvariable = currentEntryVariable))
currentEntry = self.entry[i]
currentEntry.grid(row = i,
column = 1,
sticky='EW')
# self.entry.bind("<Return>", self.OnPressEnter)
# One Button to say that everything is ok
button = tkinter.Button(self,
text = "OK",
command = self.OnButtonClick)
button.grid(row = len(self.theText),
column = 0)
self.grid_columnconfigure(0,
weight=1)
self.resizable(True,
False)
self.update()
self.geometry(self.geometry())
# self.entry.focus_set()
# self.entry.selection_range(0, tkinter.END)
def OnButtonClick(self):
global answers
answers = [x.get() for x in self.entryVariable]
# MON PROBLEME EST ICI !!!!
# Tout fermer maintenant.
def ask(*args):
global answers
answers = ['']*len(args)
app = ask_tk(args)
app.title('')
a = app.mainloop()
return answers
if __name__ == "__main__":
a,b,c = ask("a =",
"b =",
"c =")
print('a = ', a)
print('b = ', b)
print('c = ', c) |
Partager