20. Tkinter¶
- tester:
from Tkinter import *
20.1. Première app¶
#!/usr/bin/python
# -*- coding: utf-8 -*-
from Tkinter import Tk, Label
win = Tk()
label = Label(win, text="Hello world")
label.pack()
win.mainloop()
20.2. Ajouter des widgets¶
#!/usr/bin/python
# -*- coding: utf-8 -*-
from Tkinter import Tk, Label, Button
win = Tk()
label = Label(win, text="Hello world")
button_quit = Button(win, text="Quit")
label.pack()
button_quit.pack()
win.mainloop()
20.3. Input widget¶
#!/usr/bin/python
# -*- coding: utf-8 -*-
from Tkinter import Tk, Label, Button, Entry, StringVar
win = Tk()
txt = StringVar()
label = Label(win, text="Type your name")
text_input = Entry(win,textvariable= txt, width=20)
button_quit = Button(win, text="Quit")
label.pack()
text_input.pack()
button_quit.pack()
win.mainloop()
20.4. Choice widget¶
#!/usr/bin/python
# -*- coding: utf-8 -*-
from Tkinter import Tk, Label, Button, Entry, StringVar, Radiobutton
win = Tk()
txt = StringVar()
var_choix = StringVar()
first_choice = Radiobutton(win, text="First", variable=var_choix, value="rouge")
second_choice = Radiobutton(win, text="Second", variable=var_choix, value="vert")
third_choice = Radiobutton(win, text="Third", variable=var_choix, value="bleu")
label = Label(win, text="Type your name")
text_input = Entry(win,textvariable= txt, width=20)
button_quit = Button(win, text="Quit", command = win.quit) #ajout d'un comportement
label.pack()
text_input.pack()
first_choice.pack()
second_choice.pack()
third_choice.pack()
button_quit.pack()
win.mainloop()
20.5. Ajout d’un comportement¶
#!/usr/bin/python
# -*- coding: utf-8 -*-
from Tkinter import Tk, Label, Button, Entry, StringVar, Radiobutton
def say():
print text_input.get()
win = Tk()
txt = StringVar()
var_choix = StringVar()
first_choice = Radiobutton(win, text="First", variable=var_choix, value="first")
second_choice = Radiobutton(win, text="Second", variable=var_choix, value="second")
third_choice = Radiobutton(win, text="Third", variable=var_choix, value="third")
label = Label(win, text="Type your name")
text_input = Entry(win,textvariable= txt, width=20)
button_validate = Button(win, text="Validate", command = say )
button_quit = Button(win, text="Quit", command = win.quit)
label.pack()
text_input.pack()
first_choice.pack()
second_choice.pack()
third_choice.pack()
button_validate.pack()
button_quit.pack()
win.mainloop()
20.6. Version POO¶
#!/usr/bin/python
# -*- coding: utf-8 -*-
import Tkinter as tk
class MyApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
txt = tk.StringVar()
self.label = tk.Label(self, text="Type your name")
self.text_input = tk.Entry(self, textvariable=txt, width=20)
self.result = tk.Label(self, text="")
self.button_validate = tk.Button(self, text="Validate", command=self.on_validate)
self.button_quit = tk.Button(self, text="Quit", command=self.quit)
self.label.pack()
self.text_input.pack()
self.result.pack()
self.button_validate.pack()
self.button_quit.pack()
def on_validate(self):
self.result.configure(text=self.text_input.get())
app = MyApp()
app.mainloop()
20.7. Disposition¶
#!/usr/bin/python
# -*- coding: utf-8 -*-
import Tkinter as tk
class MyApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
txt = tk.StringVar()
self.label = tk.Label(self, text="Type your name")
self.text_input = tk.Entry(self, textvariable=txt, width=20)
self.result = tk.Label(self, text="")
self.button_validate = tk.Button(self, text="Validate", command=self.on_validate)
self.button_quit = tk.Button(self, text="Quit", command=self.quit)
self.label.grid(row=0, column=0)
self.text_input.grid(row=0, column=1)
self.result.grid(row=1, column=1)
self.button_validate.grid(row=2, column=0)
self.button_quit.grid(row=2, column=1)
def on_validate(self):
self.result.configure(text=self.text_input.get())
app = MyApp()
app.mainloop()