2022年1月8日 星期六

Python Tkinter Progressbar

 Python Tkinter  Progressbar

widget_object = Progressbar(parent, **options)

# importing tkinter module
from tkinter import *
from tkinter.ttk import *
# creating tkinter window
root = Tk()
root.geometry("400x320")
root.title('Progressbar widget in Tkinter')
# Progress bar widget
progress = Progressbar(root, orient = HORIZONTAL,
length = 100, mode = 'determinate')
# Function responsible for the updation
# of the progress bar value
def bar():
import time
progress['value'] = 20
root.update_idletasks()
time.sleep(1)
progress['value'] = 40
root.update_idletasks()
time.sleep(1)
progress['value'] = 50
root.update_idletasks()
time.sleep(1)
progress['value'] = 60
root.update_idletasks()
time.sleep(1)
progress['value'] = 80
root.update_idletasks()
time.sleep(1)
progress['value'] = 100
progress.pack(pady = 10)
# This button will initialize
# the progress bar
Button(root, text = 'Start', command = bar).pack(pady = 10)
Button(root, text = 'Exit', command = root.destroy).pack(pady = 10)
# infinite loop
mainloop()



# importing tkinter module
from tkinter import *
from tkinter.ttk import *
# creating tkinter window
root = Tk()
root.geometry("400x320")
root.title('Progressbar widget in Tkinter')
# Progress bar widget
progress = Progressbar(root, orient = HORIZONTAL,
length = 100, mode = 'indeterminate')
# Function responsible for the updation
# of the progress bar value
def bar():
import time
progress['value'] = 20
root.update_idletasks()
time.sleep(0.5)
progress['value'] = 40
root.update_idletasks()
time.sleep(0.5)
progress['value'] = 50
root.update_idletasks()
time.sleep(0.5)
progress['value'] = 60
root.update_idletasks()
time.sleep(0.5)
progress['value'] = 80
root.update_idletasks()
time.sleep(0.5)
progress['value'] = 100
root.update_idletasks()
time.sleep(0.5)
progress['value'] = 80
root.update_idletasks()
time.sleep(0.5)
progress['value'] = 60
root.update_idletasks()
time.sleep(0.5)
progress['value'] = 50
root.update_idletasks()
time.sleep(0.5)
progress['value'] = 40
root.update_idletasks()
time.sleep(0.5)
progress['value'] = 20
root.update_idletasks()
time.sleep(0.5)
progress['value'] = 0

progress.pack(pady = 10)
# This button will initialize
# the progress bar
Button(root, text = 'Start', command = bar).pack(pady = 10)
Button(root, text = 'Exit', command = root.destroy).pack(pady = 10)

# infinite loop
mainloop()


#========================

from tkinter import *
from tkinter.ttk import *
import time
root = Tk()
root.title('PythonGuides')
root.geometry('400x250+1000+300')
def step():
    for i in range(5):
        root.update_idletasks()
        pb1['value'] += 20
        
        time.sleep(1)
pb1 = Progressbar(root, orient=HORIZONTAL, length=100, mode='indeterminate')
pb1.pack(expand=True)
Button(root, text='Start', command=step).pack(pady = 10)
Button(root, text = 'Exit', command = root.destroy).pack(pady = 10)

root.mainloop()

#==========================

from tkinter import *
from tkinter.ttk import *
import time
root = Tk()
root.title('PythonGuides')
root.geometry('400x250+1000+300')
def step():
    for i in range(5):
        root.update_idletasks()
        pb1['value'] += 20
        time.sleep(0.5)
    
    for i in range(5):
        root.update_idletasks()
        pb1['value'] -= 20       
        time.sleep(0.5)   

pb1 = Progressbar(root, orient=HORIZONTAL, length=100, mode='indeterminate')
pb1.pack(expand=True)
Button(root, text='Start', command=step).pack(pady = 10)
Button(root, text = 'Exit', command = root.destroy).pack(pady = 10)
root.mainloop()

#===============================
from tkinter import *
from tkinter.ttk import Progressbar
import time


root = Tk()
root.title('PythonGuides')
root.geometry('400x350')
root.config(bg='#345')


pb1 = Progressbar(
    root,
    orient = HORIZONTAL,
    length = 50,
    mode = 'determinate'
    )

pb1.place(x=40, y=20)

pb2 = Progressbar(
    root,
    orient = HORIZONTAL,
    length = 100,
    mode = 'indeterminate'
    )

pb2.place(x=40, y=80)

pb3 = Progressbar(
    root,
    orient = HORIZONTAL,
    length = 150,
    mode = 'determinate'
    )

pb3.place(x=40, y=140)

pb1.start()
pb2.start()
pb3.start()

root.mainloop()

Python Tkinter – SpinBox

 Python Tkinter – SpinBox


w = Spinbox ( master, options)

Parameters:

  • master: This parameter is used to represents the parent window.
  • options:There are many options which are available and they can be used as key-value pairs separated by commas.

Options:
Following are commonly used Option can be used with this widget :-

  • activebackground: This option used to represent the background color when the slider and arrowheads is under the cursor.
  • bg: This option used to represent the normal background color displayed behind the label and indicator.
  • bd: This option used to represent the size of the border around the indicator and the default value is 2 pixels.
  • command: This option is associated with a function to be called when the state is changed.
  • cursor: By using this option, the mouse cursor will change to that pattern when it is over the type.
  • disabledforeground: This option used to represent the foreground color of the widget when it is disabled..
  • disabledbackground: This option used to represent the background color of the widget when it is disabled..
  • font: This option used to represent the font used for the text.
  • fg: This option used to represent the color used to render the text.
  • format: This option used to formatting the string and it’s has no default value.
  • from_: This option used to represent the minimum value.
  • justify: This option used to control how the text is justified: CENTER, LEFT, or RIGHT.
  • relief: This option used to represent the type of the border and It’s default value is set to SUNKEN.
  • repeatdelay: This option is used to control the button auto repeat and its default value is in milliseconds.
  • repeatinterval: This option is similar to repeatdelay.
  • state: This option used to represent the represents the state of the widget and its default value is NORMAL.
  • textvariable: This option used to control the behaviour of the widget text.
  • to: It specify the maximum limit of the widget value. The other is specified by the from_ option.
  • validate: This option is used to control how the widget value is validated.
  • validatecommand: This option is associated to the function callback which is used for the validation of the widget content.
  • values: This option used to represent the tuple containing the values for this widget.
  • vcmd: This option is same as validation command.
  • width: This option is used to represents the width of the widget.
  • wrap: This option wraps up the up and down button the Spinbox.
  • xscrollcommand: This options is set to the set() method of scrollbar to make this widget horizontally scrollable.

Methods:
Methods used in this widgets are as follows:

  • delete(startindex, endindex): This method is used to delete the characters present at the specified range.
  • get(startindex, endindex): This method is used to get the characters present in the specified range.
  • identify(x, y): This method is used to identify the widget’s element within the specified range.
  • index(index): This method is used to get the absolute value of the given index.
  • insert(index, string): This method is used to insert the string at the specified index.
  • invoke(element): This method is used to invoke the callback associated with the widget.

from tkinter import *

root = Tk()
root.geometry("300x200")

w = Label(root, text ='GeeksForGeeks', font = "50")
w.pack()

sp = Spinbox(root, from_= 0, to = 20)
sp.pack()

root.mainloop()


#==============================
from tkinter import *

root = Tk()
root.geometry("300x200")
root.title('Testing Values')

item_1 = IntVar()

def print_item_values():
    a = item_1.get()
    print(a)

item_1 = Spinbox(root, from_= 0, to = 10, width = 5)
item_1.grid(row = 10, column = 10)

value_button = Button(root, text = 'Print values', width = 20, command = 
print_item_values)
value_button.grid(row = 0, column = 1)

root.mainloop()

PythonTkinter - Menu

 PythonTkinter - Menu  


# importing only those functions
# which are needed
from tkinter import *
from tkinter.ttk import *
from time import strftime
# creating tkinter window
root = Tk()
root.title('Menu Demonstration')
root.geometry("640x320")
# Creating Menubar
menubar = Menu(root)
# Adding File Menu and commands
file = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='File', menu = file)
file.add_command(label ='New File', command = None)
file.add_command(label ='Open...', command = None)
file.add_command(label ='Save', command = None)
file.add_separator()
file.add_command(label ='Exit', command = root.destroy)
# Adding Edit Menu and commands
edit = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Edit', menu = edit)
edit.add_command(label ='Cut', command = None)
edit.add_command(label ='Copy', command = None)
edit.add_command(label ='Paste', command = None)
edit.add_command(label ='Select All', command = None)
edit.add_separator()
edit.add_command(label ='Find...', command = None)
edit.add_command(label ='Find again', command = None)
# Adding Help Menu
help_ = Menu(menubar, tearoff = 0)
menubar.add_cascade(label ='Help', menu = help_)
help_.add_command(label ='Tk Help', command = None)
help_.add_command(label ='Demo', command = None)
help_.add_separator()
help_.add_command(label ='About Tk', command = None)
# display Menu
root.config(menu = menubar)
mainloop()


2022年1月7日 星期五

Python Tkinter – Message

 Python Tkinter – Message

w = Message( master, options)

Parameters:

  • master: This parameter is used to represents the parent window.
  • options:There are many options which are available and they can be used as key-value pairs separated by commas.

Options:
Following are commonly used Option can be used with this widget :-

  • anchor: This option is used to decide the exact position of the text within the space .Its default value is CENTER.
  • bg: This option used to represent the normal background color.
  • bitmap: This option used to display a monochrome image.
  • bd: This option used to represent the size of the border and the default value is 2 pixels.
  • cursor: By using this option, the mouse cursor will change to that pattern when it is over type.
  • font: This option used to represent the font used for the text.
  • fg: This option used to represent the color used to render the text.
  • height: This option used to represent the number of lines of text on the message.
  • image: This option used to display a graphic image on the widget.
  • justify: This option used to control how the text is justified: CENTER, LEFT, or RIGHT.
  • padx: This option used to represent how much space to leave to the left and right of the widget and text. It’s default value is 1 pixel.
  • pady: This option used to represent how much space to leave above and below the widget. It’s default value is 1 pixel.
  • relief: The type of the border of the widget. It’s default value is set to FLAT.
  • text: This option used use newlines (“\n”) to display multiple lines of text.
  • variable: This option used to represents the associated variable that tracks the state of the widget.
  • width: This option used to represents the width of the widget. and also represented in the number of characters that are represented in the form of texts.
  • wraplength: This option will be broken text into the number of pieces.

from tkinter import *
root = Tk()
root.geometry("300x200")
w = Label(root, text ='GeeksForGeeks', font = "50")
w.pack()

msg = Message( root, text = "A computer science portal for geeks")

msg.pack()
root.mainloop()


Python Tkinter 圓形面積計算

 Python Tkinter 圓形面積計算



請撰寫一程式,輸入一圓的半徑,並加以計算此圓之面積和周長,最後請印出此圓的半徑(Radius)、周長(Perimeter)和面積(Area)。


'''
請撰寫一程式,輸入一圓的半徑,並加以計算此圓之面積和周長,
最後請印出此圓的半徑(Radius)、周長(Perimeter)和面積(Area)。
'''
def exit():
    root.destroy()
 
def convert():
    radius = int(e1.get())
    Perimeter = round((2 * math.pi * radius),2)
    Area= round((math.pi * radius ** 2),2)
    
    t1.config(state='normal')
    t1.delete('1.0', tk.END)
    t1.insert(tk.END,radius)
    t1.config(state='disabled')
    
    t2.config(state='normal')
    t2.delete('1.0', tk.END)
    t2.insert(tk.END,Perimeter)
    t2.config(state='disabled')
    
    t3.config(state='normal')
    t3.delete('1.0', tk.END)
    t3.insert(tk.END,Area)
    t3.config(state='disabled')
    
import math
import tkinter as tk
root = tk.Tk()
root.geometry("640x320")
root.config(bg="#A569BD")
root.resizable(width=False,height=False)
root.title('圓形面積計算')
l1= tk.Label(root,text="輸入半徑 Radius: ",font=("Arial", 10,"bold"),fg="white",bg="#A569BD")
l2= tk.Label(root,text="半徑(Radius)為 : ",font=("Arial", 10,"bold"),fg="white",bg="#A569BD")
l3= tk.Label(root,text="周長(Perimeter)為 : ",font=("Arial", 10,"bold"),fg="white",bg="#A569BD")
l4= tk.Label(root,text="面積(Area)為 : ",font=("Arial", 10,"bold"),fg="white",bg="#A569BD")
 
empty_l1 = tk.Label(root,bg="#A569BD")
empty_l2 = tk.Label(root,bg="#A569BD")
empty_l3 = tk.Label(root,bg="#A569BD")
empty_l4 = tk.Label(root,bg="#A569BD")
empty_l5 = tk.Label(root,bg="#A569BD")
e1= tk.Entry(root,font=('Arial',10))
 
btn1 = tk.Button(root,text="計算",font=("Arial", 10),command=convert)
btn2 = tk.Button(root,text="離開",font=("Arial", 10),command=exit)
 
t1=tk.Text(root,state="disabled",width=15,height=0)
t2=tk.Text(root,state="disabled",width=15,height=0)
t3=tk.Text(root,state="disabled",width=15,height=0)
l1.pack()

e1.pack()
empty_l1.pack()
btn1.pack()
l2.pack()
t1.pack()
empty_l2.pack()
l3.pack()
t2.pack()
empty_l3.pack()
l4.pack()
t3.pack()
empty_l4.pack()
btn2.pack()
root.mainloop()


Python Tkinter: Celsius to Fahrenheit Converter

 Python Tkinter: Celsius to Fahrenheit Converter




def exit():
    root.destroy()
 
def convert():
    c = int(e1.get())
    f = ((c*9)/(5))+32
    t1.config(state='normal')
    t1.delete('1.0', tk.END)
    t1.insert(tk.END,f)
    t1.config(state='disabled')
 
import tkinter as tk
root = tk.Tk()
root.geometry("320x320")
root.config(bg="#A569BD")
root.resizable(width=False,height=False)
root.title('攝氏轉華氏')
 
l1 = tk.Label(root,text="攝氏轉華氏(C2F)",font=("Arial", 15),fg="white",bg="black")
l2= tk.Label(root,text="輸入攝氏溫度 °C : ",font=("Arial", 10,"bold"),fg="white",bg="#A569BD")
l3= tk.Label(root,text="華氏(°F)為 : ",font=("Arial", 10,"bold"),fg="white",bg="#A569BD")
 
empty_l1 = tk.Label(root,bg="#A569BD")
empty_l2 = tk.Label(root,bg="#A569BD")
 
e1= tk.Entry(root,font=('Arial',10))
 
btn1 = tk.Button(root,text="轉為華氏(°F) ",font=("Arial", 10),command=convert)
btn2 = tk.Button(root,text="離開",font=("Arial", 10),command=exit)
 
t1=tk.Text(root,state="disabled",width=15,height=0)
 
l1.pack()
l2.pack()
e1.pack()
empty_l1.pack()
btn1.pack()
l3.pack()
t1.pack()
empty_l2.pack()
btn2.pack()
 
root.mainloop()


Python Tkinter – Text

 Python Tkinter – Text

T = Text(root, bg, fg, bd, height, width, font, ..)

Optional parameters

  • root – root window.
  • bg – background colour
  • fg – foreground colour
  • bd – border of widget.
  • height – height of the widget.
  • width – width of the widget.
  • font – Font type of the text.
  • cursor – The type of the cursor to be used.
  • insetofftime – The time in milliseconds for which the cursor blink is off.
  • insertontime – the time in milliseconds for which the cusrsor blink is on.
  • padx – horizontal padding.
  • pady – vertical padding.
  • state – defines if the widget will be responsive to mouse or keyboards movements.
  • highligththickness – defines the thickness of the focus highlight.
  • insertionwidth – defines the width of insertion character.
  • relief – type of the border which can be SUNKEN, RAISED, GROOVE and RIDGE.
  • yscrollcommand – to make the widget vertically scrollable.
  • xscrollcommand – to make the widget horizontally scrollable.

Some Common methods

  • index(index) – To get the specified index.
  • insert(index) – To insert a string at a specified index.
  • see(index) – Checks if a string is visible or not at a given index.
  • get(startindex, endindex) – to get characters within a given range.
  • delete(startindex, endindex) – deletes characters within specified range.

Tag handling methods

  • tag_delete(tagname) – To delete a given tag.
  • tag_add(tagname, startindex, endindex) – to tag the string in the specified range
  • tag_remove(tagname, startindex, endindex) – to remove a tag from specified range

Mark handling methods

  • mark_names() – to get all the marks in the given range.
  • index(mark) – to get index of a mark.
  • mark_gravity() – to get the gravity of a given mark.


#=============================================


#=============================================
from tkinter import *

root = Tk()
root.geometry("640x320")
root.title(" Q&A ")

def Take_input():
INPUT = inputtxt.get("1.0", "end-1c")
print(INPUT)
if(INPUT == "120"):
Output.insert(END, 'Correct')
else:
Output.insert(END, "Wrong answer") 

l = Label(text = "What is 24 * 5 ? ")
inputtxt = Text(root, height = 10,
width = 25,
bg = "light yellow")

Output = Text(root, height = 5,
width = 25,
bg = "light cyan")

Display = Button(root, height = 2,
width = 20,
text ="Show",
command = lambda:Take_input())

l.pack()
inputtxt.pack()
Display.pack()
Output.pack()

mainloop()

  清除項目編譯快取 執行 flutter clean 重新取得套件依賴 執行 flutter pub get 完整重新編譯並執行 flutter run 1. 新增專案套件 (pubspec.yaml) 在 Flutter 專案根目錄下的 pubspec.yaml 中,新增 ...