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()
ช่อง Height ให้ใส่ความสูง
ช่อง Depth ให้ใส่ความลึก
จากนั้นกดปุ่ม Calculate
ผลลัพธ์ที่ได้จะแสดงในช่อง Result
ในส่วนคำสั่งการทำงานนั้นจะต้องทำการช่อง Width Height Depth Result ด้วยคำสั่งtextbox ส่วนชื่อที่อยู่ด้านหน้าให้สร้างเป็น Label โดยคำนวณตัวเลขโดยกดปุ่ม Calculate เพื่อคำนวณผลลัพธ์ออกมา และการทำคำนวณหาค่าพื้นที่ลูกบาศก์นั้นจะต้องใส่ตัวเลขเท่านั้นแต่ Depth ความลึกนั้นไม่ต้องใส่ก็ได้
ช่องทางการศึกษาเพิ่มเติมข่าวที่น่าสนใจเกี่ยวกับ : คำนวณพื้นที่ลูกบาศก์โดยภาษา Python
-บทเรียน Python
-บทเรียน Python Framework Flask
-ความหมายของคำ คืออะไร
-VDO Tutorial - Python
-ถาม-ตอบ Java & Python