第8课开关量的或运算 python演示程序 义务教育版(2024)信息科技六年级全一册

资源下载
  1. 二一教育资源

第8课开关量的或运算 python演示程序 义务教育版(2024)信息科技六年级全一册

资源简介

"""
逻辑或运算(OR)演示程序 - 图形窗口版
=====================================
基于 tkinter 的图形窗口程序,演示逻辑或运算:
A ──┐
├─ OR ──> Y = A or B
B ──┘
窗口包含三个选项卡:
1. 真值表 :完整展示 A、B 与 Y 的对应关系
2. 手动调试 :下拉选择 A、B 两个信号,点击"计算"查看结果(指示灯)
3. 场景模拟 :勾选传感器状态,演示"任一触发即报警"的或运算应用
运行方式:
python logic_or_gui.py
说明:tkinter 为 Python 自带标准库,无需额外安装。
"""
import tkinter as tk
from tkinter import ttk
class LogicOrApp:
"""逻辑或运算演示窗口"""
def __init__(self, root: tk.Tk) -> None:
root.title("逻辑或运算(OR)演示程序")
root.geometry("560x430")
root.resizable(False, False)
# 顶部标题与说明
title = ttk.Label(root, text="逻辑或运算(OR)演示", font=("", 18, "bold"))
title.pack(pady=(12, 2))
sub = ttk.Label(root, text="Y = A or B (任一输入为 1,输出即为 1)",
font=("", 10), foreground="#555555")
sub.pack(pady=(0, 8))
# 选项卡容器
notebook = ttk.Notebook(root)
notebook.pack(fill="both", expand=True, padx=12, pady=5)
self.tab_truth = ttk.Frame(notebook)
self.tab_test = ttk.Frame(notebook)
self.tab_scene = ttk.Frame(notebook)
notebook.add(self.tab_truth, text=" 真值表 ")
notebook.add(self.tab_test, text=" 手动调试 ")
notebook.add(self.tab_scene, text=" 场景模拟 ")
self._build_truth_table()
self._build_test_tab()
self._build_scene_tab()
# ---------- 选项卡1:真值表 ----------
def _build_truth_table(self) -> None:
frame = self.tab_truth
ttk.Label(frame, text=" A B | Y = A or B",
font=("", 13, "bold")).pack(pady=(20, 12))
for a in (0, 1):
for b in (0, 1):
y = a or b
color = "#2e7d32" if y else "#757575"
text = f" {a} {b} | {y}"
ttk.Label(frame, text=text, font=("Courier", 13),
foreground=color).pack(pady=2)
ttk.Label(frame, text="特点:只要有一个输入为 1,输出就是 1。",
font=("", 10), foreground="#555555").pack(pady=(14, 0))
# ---------- 选项卡2:手动调试 ----------
def _build_test_tab(self) -> None:
frame = self.tab_test
ttk.Label(frame, text="选择 A、B 两个信号的值,然后点击【计算】",
font=("", 11)).pack(pady=(16, 10))
# A 选择
row_a = ttk.Frame(frame)
row_a.pack(pady=6)
ttk.Label(row_a, text="信号 A =", font=("", 12)).pack(side="left", padx=6)
self.var_a = tk.StringVar(value="0")
bobox(row_a, textvariable=self.var_a, values=("0", "1"),
width=5, state="readonly").pack(side="left")
# B 选择
row_b = ttk.Frame(frame)
row_b.pack(pady=6)
ttk.Label(row_b, text="信号 B =", font=("", 12)).pack(side="left", padx=6)
self.var_b = tk.StringVar(value="0")
bobox(row_b, textvariable=self.var_b, values=("0", "1"),
width=5, state="readonly").pack(side="left")
ttk.Button(frame, text="计算 Y = A or B",
command=self._on_calc).pack(pady=16)
self.result_label = ttk.Label(frame, text="Y = ",
font=("", 22, "bold"))
self.result_label.pack(pady=8)
def _on_calc(self) -> None:
a = int(self.var_a.get())
b = int(self.var_b.get())
y = a or b
if y:
self.result_label.config(text="Y = 1 灯亮", foreground="#2e7d32")
else:
self.result_label.config(text="Y = 0 灯灭", foreground="#757575")
# ---------- 选项卡3:场景模拟 ----------
def _build_scene_tab(self) -> None:
frame = self.tab_scene
ttk.Label(frame, text="小区报警系统:任一传感器触发即报警(或运算)",
font=("", 11)).pack(pady=(16, 10))
self.var_noise = tk.BooleanVar(value=False)
self.var_door = tk.BooleanVar(value=False)
ttk.Checkbutton(frame, text="① 噪声传感器触发(≥55dB)",
variable=self.var_noise).pack(anchor="w", padx=36, pady=5)
ttk.Checkbutton(frame, text="② 门磁传感器触发(门被打开)",
variable=self.var_door).pack(anchor="w", padx=36, pady=5)
ttk.Button(frame, text="检查是否报警",
command=self._on_check).pack(pady=16)
self.scene_label = ttk.Label(frame, text="请选择传感器状态后检查",
font=("", 14, "bold"))
self.scene_label.pack(pady=8)
def _on_check(self) -> None:
noise = self.var_noise.get()
door = self.var_door.get()
y = noise or door
if y:
self.scene_label.config(
text=" 报警器响起!请管理人员处理!", foreground="#d32f2f")
else:
self.scene_label.config(
text=" 一切正常,无需处理。", foreground="#2e7d32")
def main() -> None:
root = tk.Tk()
LogicOrApp(root)
root.mainloop()
if __name__ == "__main__":
main()

展开更多......

收起↑

资源预览