โดย ichimarusoichi » 04/02/2019 6:47 pm
วันนี้ผมจะมานำเสนอการใช้
Python มาคำนวณพื้นลูกบาศก์โดยมีหน่วยเป็นเซนติเมตรขั้นตอนในการทำงานนั้นจะในคำสั่งของ
Labelและ
textboxลองทำความเข้าใจในคำสั่งและลองเขียนได้เลยครับ
Input
โค้ด: เลือกทั้งหมด
from guizero import App, Text, TextBox, PushButton, error
def calculate():
# ตรวจสอบชุดข้อมูลที่กรอกต้องเป็นตัวเลข
if not height.value.isdigit() or not width.value.isdigit():
error("Input error", "You must type in numbers for height and width")
# ในส่วนของความลึกนั้นไม่ต้องก็ได้
elif not depth.value.isdigit() and depth.value != "":
error("Input error", "You must type in a number for depth")
#คำนวณพื้นที่ลูกบาศก์
else:
area = int(height.value) * int(width.value)
if depth.value == "":
result.value = str(area) + "cm squared"
else:
volume = area * int(depth.value)
result.value = str(volume) + "cm cubed"
#กำหนดแบบฟอร์มหน้าจอการทำงาน
app = App("เครื่องช่วยคำนวณหาพื้นที่ลูกบาศก์", layout="grid")
width_label = Text(app, text="Width:", grid=[0, 0], align="left")
width = TextBox(app, grid=[1, 0], align="left", width=30)
height_label = Text(app, text="Height:", grid=[0, 1], align="left")
height = TextBox(app, grid=[1, 1], align="left", width=30)
depth_label = Text(app, text="Depth:", grid=[0, 2], align="left")
depth = TextBox(app, grid=[1, 2], align="left", width=30)
button = PushButton(app, calculate, text="Calculate", grid=[1, 3])
result_label = Text(app, text="Result:", grid=[0, 4], align="left")
result = TextBox(app, grid=[1, 4], align="left", width=30)
app.display()
Output
- หน้าจอเครื่องช่วยคำนวณพื้นที่ลูกบาศก์
- areabox.PNG (4.52 KiB) Viewed 2048 times
ช่อง Width ให้ใส่ความกว้าง
ช่อง Height ให้ใส่ความสูง
ช่อง Depth ให้ใส่ความลึก
จากนั้นกดปุ่ม Calculate
ผลลัพธ์ที่ได้จะแสดงในช่อง Result
ในส่วนคำสั่งการทำงานนั้นจะต้องทำการช่อง Width Height Depth Result ด้วยคำสั่งtextbox ส่วนชื่อที่อยู่ด้านหน้าให้สร้างเป็น Label โดยคำนวณตัวเลขโดยกดปุ่ม Calculate เพื่อคำนวณผลลัพธ์ออกมา และการทำคำนวณหาค่าพื้นที่ลูกบาศก์นั้นจะต้องใส่ตัวเลขเท่านั้นแต่ Depth ความลึกนั้นไม่ต้องใส่ก็ได้
ช่องทางการศึกษาเพิ่มเติมข่าวที่น่าสนใจเกี่ยวกับ : คำนวณพื้นที่ลูกบาศก์โดยภาษา Python
-บทเรียน Python
-บทเรียน Python Framework Flask
-ความหมายของคำ คืออะไร
-VDO Tutorial - Python
-ถาม-ตอบ Java & Python
วันนี้ผมจะมานำเสนอการใช้ [url=https://www.mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3/2417-python-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html]Python[/url] มาคำนวณพื้นลูกบาศก์โดยมีหน่วยเป็นเซนติเมตรขั้นตอนในการทำงานนั้นจะในคำสั่งของ [url=https://www.mindphp.com/forums/viewtopic.php?f=144&t=45794]Label[/url]และ [url=https://www.mindphp.com/forums/viewtopic.php?f=16&t=53074]textbox[/url]ลองทำความเข้าใจในคำสั่งและลองเขียนได้เลยครับ
[b]Input[/b]
[code]from guizero import App, Text, TextBox, PushButton, error
def calculate():
# ตรวจสอบชุดข้อมูลที่กรอกต้องเป็นตัวเลข
if not height.value.isdigit() or not width.value.isdigit():
error("Input error", "You must type in numbers for height and width")
# ในส่วนของความลึกนั้นไม่ต้องก็ได้
elif not depth.value.isdigit() and depth.value != "":
error("Input error", "You must type in a number for depth")
#คำนวณพื้นที่ลูกบาศก์
else:
area = int(height.value) * int(width.value)
if depth.value == "":
result.value = str(area) + "cm squared"
else:
volume = area * int(depth.value)
result.value = str(volume) + "cm cubed"
#กำหนดแบบฟอร์มหน้าจอการทำงาน
app = App("เครื่องช่วยคำนวณหาพื้นที่ลูกบาศก์", layout="grid")
width_label = Text(app, text="Width:", grid=[0, 0], align="left")
width = TextBox(app, grid=[1, 0], align="left", width=30)
height_label = Text(app, text="Height:", grid=[0, 1], align="left")
height = TextBox(app, grid=[1, 1], align="left", width=30)
depth_label = Text(app, text="Depth:", grid=[0, 2], align="left")
depth = TextBox(app, grid=[1, 2], align="left", width=30)
button = PushButton(app, calculate, text="Calculate", grid=[1, 3])
result_label = Text(app, text="Result:", grid=[0, 4], align="left")
result = TextBox(app, grid=[1, 4], align="left", width=30)
app.display()[/code]
[b]Output[/b]
[attachment=0]areabox.PNG[/attachment]
ช่อง Width ให้ใส่ความกว้าง
ช่อง Height ให้ใส่ความสูง
ช่อง Depth ให้ใส่ความลึก
จากนั้นกดปุ่ม Calculate
ผลลัพธ์ที่ได้จะแสดงในช่อง Result
ในส่วนคำสั่งการทำงานนั้นจะต้องทำการช่อง Width Height Depth Result ด้วยคำสั่งtextbox ส่วนชื่อที่อยู่ด้านหน้าให้สร้างเป็น Label โดยคำนวณตัวเลขโดยกดปุ่ม Calculate เพื่อคำนวณผลลัพธ์ออกมา และการทำคำนวณหาค่าพื้นที่ลูกบาศก์นั้นจะต้องใส่ตัวเลขเท่านั้นแต่ Depth ความลึกนั้นไม่ต้องใส่ก็ได้
[b]ช่องทางการศึกษาเพิ่มเติมข่าวที่น่าสนใจเกี่ยวกับ : คำนวณพื้นที่ลูกบาศก์โดยภาษา Python[/b]
[url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/83-python.html]-บทเรียน Python[/url]
[url=https://www.mindphp.com/%E0%B8%9A%E0%B8%97%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B8%AD%E0%B8%AD%E0%B8%99%E0%B9%84%E0%B8%A5%E0%B8%99%E0%B9%8C/python-framework-flask.html]-บทเรียน Python Framework Flask[/url]
[url=https://www.mindphp.com/%E0%B8%84%E0%B8%B9%E0%B9%88%E0%B8%A1%E0%B8%B7%E0%B8%AD/73-%E0%B8%84%E0%B8%B7%E0%B8%AD%E0%B8%AD%E0%B8%B0%E0%B9%84%E0%B8%A3.html]-ความหมายของคำ คืออะไร[/url]
[url=https://mindphp.com/vdo-tutorial-python.html]-VDO Tutorial - Python[/url]
[url=https://www.mindphp.com/forums/viewforum.php?f=16]-ถาม-ตอบ Java & Python[/url]