




السمعة:
- إنضم22 يناير 2024
- المشاركات 227
- الحلول 3
- مستوى التفاعل 525
- النقاط 93
السلام عليكم ورحمة الله وبركاته
في هذا البرنامج أهدف الى تحويل Binary set الى نوع معين من signal حسب اختيار المستخدم فشكله وطريقة رسمه تختلف من signal الى أخرى.
هذه أول مرة لي على الإطلاق أتعامل فيها مع لغة Python
أمس كانت أول مرة لي ومحتاجة أغير لون white blank إلى نفس لون خلفية الشاشة كنت قد جربت ذلك لكنها تؤثر على output plot
ولا يظهر بعد ذلك.
بحثت كثيرًا ولم اجد حل! أتمنى المُساعدة، محتاجة أسلم الواجب في مادة الشبكات في ظرف قصير ويعطيكم العافية
Python:
import customtkinter
from tkinter import *
from tkinter.ttk import Combobox
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme("blue")
window = customtkinter.CTk()
window.title("Mina Devlopment")
window.geometry("1000x600")
frame = customtkinter.CTkFrame(window)
frame.pack(fill=BOTH, expand=True)
label_binary = customtkinter.CTkLabel(frame, text="Enter binary number:", font=('Helvetica', 20, 'bold'))
label_binary.pack(pady=(50, 10))
def validate_binary_input(text):
return all(char in '01' for char in text)
entry_binary = customtkinter.CTkEntry(frame, width=300, placeholder_text="Binary number", validate='key',justify='center')
entry_binary.pack(pady=10)
vcmd = (window.register(validate_binary_input), '%P')
entry_binary.configure(validatecommand=vcmd)
label_plot = customtkinter.CTkLabel(frame, text="Choose plot type:", font=('Helvetica', 20, 'bold'))
label_plot.pack(pady=(10, 10))
box = customtkinter.CTkComboBox(frame, values=["Manchester", "Miller", "NRZ", "NRZ_I", "RZ", "Manchester differential"],justify='center')
box.pack(pady=(10, 20))
# Create a canvas for plotting
canvas = customtkinter.CTkCanvas(frame, width=800, height=300)
canvas.pack(pady=20)
# Placeholder for the current plot object
current_plot = None
def plot_selected():
global current_plot # Use global variable to track current plot
plot = box.get()
binary = entry_binary.get()
# Clear previous plot if exists
if current_plot:
current_plot.get_tk_widget().destroy() # Destroy previous plot widget
# Prepare new figure and plot
fig, ax = plt.subplots(figsize=(8, 3))
if plot == "NRZ":
NRZ_plot(binary, ax)
elif plot == "RZ":
RZ_plot(binary, ax)
elif plot == "Manchester":
Manchester_plot(binary, ax)
elif plot == "Manchester differential":
Manchester_differential_plot(binary, ax)
elif plot == "NRZ_I":
NRZ_I_plot(binary, ax)
elif plot == "Miller":
Miller_plot(binary, ax)
# Display new plot on canvas
canvas_agg = FigureCanvasTkAgg(fig, master=canvas)
canvas_agg.draw()
canvas_agg.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
# Update current_plot to track the new plot
current_plot = canvas_agg
def NRZ_plot(signal, ax):
# Plot NRZ on the provided axes
x_values = []
y_values = []
y = 0
for num in signal:
x_values.append(y)
y += 1
y_values.append(int(num))
ax.step(x_values, y_values, color='blue')
ax.set_title("NRZ: " + signal)
ax.set_ylim(-1.1, 1.1)
ax.grid(True)
def RZ_plot(signal, ax):
# Plot RZ on the provided axes
x = []
y = []
z = 0.5
for num in signal:
x.append(z)
z += 0.5
x.append(z)
z += 0.5
if num == "0":
y.extend([-1, 0])
else:
y.extend([1, 0])
ax.step(x, y, where='post', color='red')
ax.set_title("RZ: " + signal)
ax.set_ylim(-1.1, 1.1)
ax.grid(True)
def Manchester_plot(signal, ax):
# Plot Manchester on the provided axes
x = []
y = []
z = 0.5
for num in signal:
x.append(z)
z += 0.5
x.append(z)
z += 0.5
if num == "0":
y.extend([1, -1])
else:
y.extend([-1, 1])
ax.step(x, y, where='post', color='yellow')
ax.set_title("Manchester: " + signal)
ax.set_ylim(-1.1, 1.1)
ax.grid(True)
def Manchester_differential_plot(signal, ax):
# Plot Manchester Differential on the provided axes
x = []
y = []
prev_bit = '0'
s = 0.5
for bit in signal:
x.extend([s, s + 0.5])
if bit == '0':
if prev_bit == '1':
y.extend([1, -1])
else:
y.extend([-1, 1])
else:
y.extend([-1, 1] if prev_bit == '1' else [1, -1])
prev_bit = bit
s += 1
ax.step(x, y, where='post', color='green')
ax.set_title("Manchester Differential: " + signal)
ax.set_ylim(-1.1, 1.1)
ax.grid(True)
def NRZ_I_plot(signal, ax):
# Plot NRZ Inverted on the provided axes
x = []
y = []
z = 1
s = 1
for bit in signal:
x.append(s)
if bit == '0':
y.append(z)
else:
y.append(-z)
z *= -1
s += 1
ax.step(x, y, where='post', color='grey')
ax.set_title("NRZ Inverted: " + signal)
ax.set_ylim(-1.1, 1.1)
ax.grid(True)
def Miller_plot(signal, ax):
# Plot Miller on the provided axes
x = []
y = []
state = 0
s = 0.5
for bit in signal:
if bit == '1':
if state == 0:
x.append(s)
y.append(1)
x.append(s + 0.5)
y.append(-1)
state = 1
else:
x.append(s)
y.append(-1)
x.append(s + 0.5)
y.append(1)
state = 0
s += 1
else:
x.append(s)
y.append(1)
x.append(s + 0.5)
y.append(1)
s += 1
ax.step(x, y, where='post')
ax.set_title("Miller: " + signal)
ax.set_ylim(-1.1, 1.1)
ax.grid(True)
btn_plot_custom = customtkinter.CTkButton(frame, text="Plot", width=100, command=plot_selected)
btn_plot_custom.pack(pady=(20, 50))
window.mainloop()