Tkinter: Difference between revisions

1,062 bytes added ,  6 years ago
(Created page with " Installing: sudo apt install python-tk sudo apt install python3-tk Importing: <syntaxhighlight lang="python"> try: from Tkinter import * # for Python2 except Imp...")
 
 
(7 intermediate revisions by the same user not shown)
Line 1:
[[Category:Scripting]]
__TOC__
<br />
 
= Basics=
 
*Installing:
sudo apt install python-tk
sudo apt install python3-tk
 
*Importing:
 
<syntaxhighlight lang="python">
Line 14 ⟶ 18:
</syntaxhighlight>
 
*Importing as per PEP8:
 
<syntaxhighlight lang="python">
Line 22 ⟶ 26:
</syntaxhighlight>
 
Or bitBit more typing but similar:
 
<syntaxhighlight lang="python">
Line 30 ⟶ 34:
</syntaxhighlight>
 
=== Hello World ==Example =
 
<syntaxhighlight lang="python">
from tkinter import Tk, Label
Line 42 ⟶ 47:
</syntaxhighlight>
 
=== ButtonsFrame ===
<syntaxhighlight lang="python">
import tkinter as tk
 
root = tk.Tk()
 
#Frame 1
frame1 = tk.Frame(root, height=300, width=200, background="red")
frame1.grid(row=0, column=0)
 
# Frame 2
tk.Frame(root, height=300, width=200, background="yellow").grid(row=0, column=1)
 
root.mainloop()
</syntaxhighlight>
 
= Buttons =
 
<syntaxhighlight lang="python">
from tkinter import Tk, Button
Line 57 ⟶ 79:
</syntaxhighlight>
 
=== Label ===
 
<syntaxhighlight lang="python">
from Tkinter import *
Line 68 ⟶ 91:
</syntaxhighlight>
 
=== Listbox ===
 
*Defining Listbox:
listbox = Listbox(root, height=30, width=100)
listbox.xview_scroll(3, "pages")
Line 75 ⟶ 99:
listbox.grid(row=7, columnspan=3)
 
*Inserting output:
listbox.insert(END, "1st Entry")
 
*Deleting Output:
listbox.delete(0, END)
 
*Auto-scrolling Scrollbar:
 
<syntaxhighlight lang="python">
from tkinter import *
Line 101 ⟶ 126:
</syntaxhighlight>
 
=== Input ===
 
<syntaxhighlight lang="python">
root = Tk()
Line 112 ⟶ 138:
</syntaxhighlight>
 
=== Output in CLI ===
 
<syntaxhighlight lang="python">
from tkinter import *
Line 125 ⟶ 152:
</syntaxhighlight>
 
=== Output in GUI ===
 
<syntaxhighlight lang="python">
from tkinter import *
Line 137 ⟶ 165:
</syntaxhighlight>
 
=== Pack Manager ===
 
<syntaxhighlight lang="python">
from Tkinter import *
Line 150 ⟶ 179:
</syntaxhighlight>
 
=== Grid Manager ===
 
<syntaxhighlight lang="python">
from tkinter import *
Line 164 ⟶ 194:
</syntaxhighlight>
 
=== Filedialog ===
 
=== Filedialog ===
<syntaxhighlight lang="python">
import tkinter
Line 180 ⟶ 210:
</syntaxhighlight>
 
=== Toplevel ===
 
Method to create Child Windows:
<syntaxhighlight lang="python">
Line 189 ⟶ 220:
def press():
win1 = Toplevel()
win1.attributes('-topmost', 'true')
Button(win1, text = "Event", command=event).grid()
Button(win1, text = "Q", command=win1.destroy).grid()
Line 200 ⟶ 232:
</syntaxhighlight>
 
== Key Bindings ==
 
<syntaxhighlight lang="python">
from tkinter import *
Line 212 ⟶ 245:
</syntaxhighlight>
 
== Progress Bar ==
 
<syntaxhighlight lang="python">
from tkinter.ttk import Progressbar
Line 222 ⟶ 256:
</syntaxhighlight>
 
== Cursors ==
 
* Over Buttons:
Line 247 ⟶ 281:
</syntaxhighlight>
 
== MessageBox ==
 
To show a message box using Tkinter, you can use messagebox library like this:
Line 254 ⟶ 288:
<syntaxhighlight lang="python">
from tkinter import messagebox
messagebox.showinfo('Message title','Message content', parent=frame)
</syntaxhighlight>
 
*Warn or Error:
<syntaxhighlight lang="python">
messagebox.showwarning('Message title', 'Message content', parent=frame) #shows warning message
messagebox.showerror('Message title', 'Message content', parent=frame) #shows error message
</syntaxhighlight>
 
*Askquestion dialogs:
<syntaxhighlight lang="python">
res = messagebox.askquestion('Message title','Message content', parent=frame)
res = messagebox.askyesno('Message title','Message content', parent=frame)
res = messagebox.askyesnocancel('Message title','Message content', parent=frame)
res = messagebox.askokcancel('Message title','Message content', parent=frame)
res = messagebox.askretrycancel('Message title','Message content', parent=frame)
</syntaxhighlight>
 
Line 277 ⟶ 311:
from tkinter import messagebox
 
windowroot = Tk()
 
def clicked():
messagebox.showinfo('Message title', 'Message content', parent=root)
 
btn = Button(windowroot,text='Click here', command=clicked)
btn.grid(column=0,row=0)
 
Line 290 ⟶ 324:
</syntaxhighlight>
 
== SpinBox ==
 
*To create a Spinbox widget, you can use Spinbox class like this:
 
spin = Spinbox(window, from_=0, to=100)
spin = Spinbox(window, from_=0, to=100, width=5)
 
*Complete example:
<syntaxhighlight lang="python">
from tkinter import *
Line 308 ⟶ 342:
</syntaxhighlight>
 
== Menu Bar ==
To add a menu bar, you can use menu class like this:
 
Line 332 ⟶ 366:
</syntaxhighlight>
 
== Notebook widget (tab control) ==
 
To create a tab control follow these steps:
Line 355 ⟶ 389:
</syntaxhighlight>
 
== Add widgets to Notebooks ==
 
After creating tabs, you can put widgets inside these tabs by assigning the parent property to the desired tab.
Line 380 ⟶ 414:
window.mainloop()
</syntaxhighlight>
 
= Display window in Center =
<syntaxhighlight lang="python">
import tkinter
 
def center(toplevel):
toplevel.update_idletasks()
w = toplevel.winfo_screenwidth()
h = toplevel.winfo_screenheight()
size = tuple(int(_) for _ in toplevel.geometry().split('+')[0].split('x'))
x = w/2 - size[0]/2
y = h/2 - size[1]/2
toplevel.geometry("%dx%d+%d+%d" % (size + (x, y)))
 
root = Tk()
 
win = Toplevel(root)
win.title("Centered!")
center(win)
 
root.mainloop()
</syntaxhighlight>
 
<br />
;References
<references/>
<br />
<br />
<br />
 
 
{{DISQUS}}