python3 gui (tkinter) on windows

ที่มา: https://www.youtube.com/watch?v=bWkwHpY7F_4&list=PLEE74DyIkwEl9Mr7GuItlE2hsAW5suKwE

ผมลองทดสอบบน windows เพราะคาดว่า ส่วนใหญ่น่าจะเป็น windows ที่ใช้งาน

ส่วนการทำไฟล์ติดตั้ง คาดว่าต้องมีแต่ แต่ยังไม่ได้ ลอง แต่จากอ่านมีหลายวิธีหลายเจ้าอยู่

http://thaimcu.blogspot.com/2015/04/install-bbfreeze-compile-py-python-file.html

https://www.skconan.ml/python-pyinstaller/

https://medium.com/@taohuh/%E0%B8%84%E0%B8%AD%E0%B8%A1%E0%B9%84%E0%B8%9E%E0%B8%A5%E0%B9%8C-python-%E0%B9%80%E0%B8%9B%E0%B9%87%E0%B8%99-exe-%E0%B8%87%E0%B9%88%E0%B8%B2%E0%B8%A2%E0%B9%83%E0%B8%99-1-%E0%B8%9A%E0%B8%A3%E0%B8%A3%E0%B8%97%E0%B8%B1%E0%B8%94-60ac5e5c6883

 

ส่วน gui  code ตามนี้

from tkinter import *
from tkinter import messagebox
from tkinter import colorchooser
from tkinter import filedialog
import sys
def mHello():
print(“aaaaaaaaa”)
def hello(event):

#messagebox
# messagebox.showinfo(title=’error’,message=”sytgeks”)
# messagebox.showwarning(title=’ชิบหายละ’,message=”ฟฟฟฟ”)
# messagebox.showerror(title=’ชิบหายละ’,message=”ฟฟฟฟ”)
# messagebox.askquestion(title=’ชิบหายละ’,message=”ฟฟฟฟ”)
# messagebox.askokcancel(title=’ชิบหายละ’,message=”ฟฟฟฟ”)
status=messagebox.askyesno(title=’ชิบหายละ’,message=”ฟฟฟฟ”)
if status>0:

sys.exit()

def fColor(event):

#Color Dialog
mycolor=colorchooser.askcolor()
mLabell=Label(text=mycolor).pack()
# mLabell.pack()

def fOpen(event):

#filedialog
myFile=filedialog.askopenfile()
mLabell=Label(text=myFile).pack()

gui=Tk()
#ขนาดจอ
gui.geometry(“450×450”)
#=ชื่อเปิด
gui.title(“server”)
#ข้อความแสดง
mlabel=Label(text=”dddddddd”,fg=”#c1c1c1″,bg=”pink”).pack()
# mlabel.pack()
#ปุ่มกด
mButton=Button(text=”click”,fg=”#c1c1c1″,bg=”pink”,command=mHello).pack()
b1=Button(text=”Hello”)
# b1.bind(‘<Button-1>’,hello)
b1.bind(‘<Double-1>’,hello)
b1.pack()

b2=Button(text=”Color”)
# b1.bind(‘<Button-1>’,hello)
b2.bind(‘<Button-1>’,fColor)
b2.pack()

b3=Button(text=”Select File:”)
# b1.bind(‘<Button-1>’,hello)
b3.bind(‘<Button-1>’,fOpen)
b3.pack()

#ช่องกรอก
objEntry=Entry().pack()
#เมนู
menubar=Menu(gui)
#เมนู file
fileMenu=Menu(menubar,tearoff=0)
fileMenu.add_command(label=”New”,command=mHello)
fileMenu.add_command(label=”Open”)
fileMenu.add_command(label=”Save AS”)
fileMenu.add_command(label=”Close”)
#เมนู help
HelpMenu=Menu(menubar,tearoff=0)
HelpMenu.add_command(label=”connect”,command=mHello)
HelpMenu.add_command(label=”document”)
#รวมเมนู
menubar.add_cascade(label=”File”,menu=fileMenu)
menubar.add_cascade(label=”Help”,menu=HelpMenu)
gui.config(menu=menubar)

#Radio button
r1=Radiobutton(text=”ชาย”,value=”1″).pack()
r2=Radiobutton(text=”หญิง”,value=”2″).pack()
#SpinBox
spin1=Spinbox(from_=-10,to=10,state=DISABLED).pack()
#Listbox
l1=Listbox()
l1.insert(1,”Python”)
l1.insert(2,”JAVA”)
l1.insert(3,”Database”)
l1.pack()
#slider
s1=Scale(orient=HORIZONTAL,width=20,from_=0,to=100,length=300,tickinterval=10)

s1.pack()
gui.mainloop()

 

อันนี้เป็นการ pack จะแสดงที่หน้าจอเลย ส่วนหากต้องการทำเป็นตารางเป็น grid ตามนี้ครับ

 

from tkinter import *
from tkinter import messagebox
from tkinter import colorchooser
from tkinter import filedialog
import sys

gui=Tk()
#ขนาดจอ
gui.geometry(“250×100”)
#=ชื่อเปิด
gui.title(“server”)
user=”ชื่อผู้ใช้:”
l1=Label(text=user)
l1.grid(row=0)
# l1.pack(side=LEFT)
l2=Label(text=”รหัสผ่าน:”)
l2.grid(row=1)
e1=Entry(bd=5)
e1.grid(row=0,column=1)
e2=Entry(bd=5)
e2.grid(row=1,column=1)
btn1=Button(text=”เข้าสู่ระบบ”)
# btn1.bind(‘<Button-1>’,fColor)
btn1.grid(row=2,column=0)
# l2.pack(side=LEFT)
# e1=Entry(bd=5).pack(side=RIGHT)
gui.mainloop()

 

อาจจะไม่ได้ อธิบายทั้งหมด แต่คอมเม้นไว้ไปลอง run แล้วลบๆแก้ดูนะ ส่วนเรื่องการดึงละส่งค่า เดียวหาอ่านอีกที

ใส่ความเห็น

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องที่ต้องการถูกทำเครื่องหมาย *