我設法創建了 3 個滑塊。每個滑塊控制 X、Y 和 Z 螺旋角。我想將滑塊的三個角度組合到一個 np.array 中,操作該陣列并將結果顯示在打開的視窗中的新網格中。我希望隨著滑塊的移動結果實時更新。我可以操縱滑塊,但我不知道如何 a)。將 3 個結果合并為一個陣列 b)。操縱這些結果 c)。顯示這些結果。我正在嘗試更新兩個不同網格位置的滑塊值,但失敗了。這是我到目前為止撰寫的代碼。任何反饋將不勝感激!
import numpy as np
import tkinter as tk
from tkinter import ttk
# root window
root = tk.Tk()
root.geometry('900x800')
# root.attributes('-fullscreen', True)
root.resizable(True, True)
root.title('Slider Demo')
# slider current value
current_value_x = tk.DoubleVar()
current_value_y = tk.DoubleVar()
current_value_z = tk.DoubleVar()
def get_current_value():
return ['{: .2f}'.format(current_value_x.get()),
'{: .2f}'.format(current_value_y.get()),
'{: .2f}'.format(current_value_z.get())]
def slider_changed(event):
value_label_x.configure(text=get_current_value()[0])
value_label_y.configure(text=get_current_value()[1])
value_label_z.configure(text=get_current_value()[2])
# label for the slider
slider_label_helical_x = ttk.Label(root, text='Helical X:')
slider_label_helical_y = ttk.Label(root, text='Helical Y:')
slider_label_helical_z = ttk.Label(root, text='Helical Z:')
slider_label_helical_x.grid(column=0, row=0, sticky='w')
slider_label_helical_y.grid(column=0, row=1, sticky='w')
slider_label_helical_z.grid(column=0, row=2, sticky='w')
# slider
slider_x = ttk.Scale(root, from_=-180, to=180, orient='horizontal', command=slider_changed, variable=current_value_x)
slider_y = ttk.Scale(root, from_=-180, to=180, orient='horizontal', command=slider_changed, variable=current_value_y)
slider_z = ttk.Scale(root, from_=-180, to=180, orient='horizontal', command=slider_changed, variable=current_value_z)
slider_x.grid(column=1, row=0, sticky='we')
slider_y.grid(column=1, row=1, sticky='we')
slider_z.grid(column=1, row=2, sticky='we')
# Helical value label
value_label_x = ttk.Label(root, text=get_current_value()[0])
value_label_x.grid(column=2, row=0, sticky='n')
value_label_y = ttk.Label(root, text=get_current_value()[1])
value_label_y.grid(column=2, row=1, sticky='n')
value_label_z = ttk.Label(root, text=get_current_value()[2])
value_label_z.grid(column=2, row=2, sticky='n')
# Rotation Matrix Labels
rotationMatrixLabelHeader = ttk.Label(root, text='Rotation Matrix')
rotationMatrixLabel_1_1 = ttk.Label(root, text='(1,1)')
rotationMatrixValue_1_1 = ttk.Label(root, text=get_current_value()[0])
rotationMatrixLabelHeader.grid(column=0, row=3, sticky='w')
rotationMatrixLabel_1_1.grid(column=0, row=4, sticky='w')
rotationMatrixValue_1_1.grid(column=0, row=5, sticky='w')
root.mainloop()
到目前為止,我已經設法將 3 個滑塊放置在我想要的位置,以及它們適當的標簽和輸出。在第 0 列第 5 行,我試圖操縱第一個滑塊的值并顯示它。目前,我什至無法顯示該滑塊的值,更不用說操縱它了。
uj5u.com熱心網友回復:
我已經根據您的原始代碼撰寫了一個有效的解決方案。下面的代碼實時顯示了“原始”Numpy 陣列和“更改后的”Nupmy 陣列(反轉)。當您更改滑塊上的值時,矩陣值會更新。
我在代碼中添加了幾條注釋,作為解釋。
代碼:
import numpy as np
import tkinter as tk
from tkinter import ttk
# root window
root = tk.Tk()
root.geometry("900x800")
# root.attributes('-fullscreen', True)
root.resizable(True, True)
root.title("Slider Demo")
# slider current value
current_value_x = tk.DoubleVar()
current_value_y = tk.DoubleVar()
current_value_z = tk.DoubleVar()
def get_current_value():
return [
"{: .2f}".format(current_value_x.get()),
"{: .2f}".format(current_value_y.get()),
"{: .2f}".format(current_value_z.get()),
]
def slider_changed(event):
current_values = get_current_value()
value_label_x.configure(text=current_values[0])
value_label_y.configure(text=current_values[1])
value_label_z.configure(text=current_values[2])
# Fill the numpy array with the "new" values when you change a slider.
# Probably here a float(X) casting would be nice.
np_arr = np.array([current_values[0], current_values[1], current_values[2]])
# Call the calculation and show method.
# You can implement the Array manipulations in this function.
calculate_and_show_matrix(np_arr)
def calculate_and_show_matrix(np_array):
"""
Here you can do anything with your Numpy array and show the value on the GUI.
Currently this function only show the original Numpy array and the reversed array.
@param np_array: The Numpy Array
@return: None
"""
# Set the "text" parameter of "rotationMatrixValue_1_1_orig" object to the "original" array value.
rotationMatrixValue_1_1_orig.configure(text="{}".format(np_array))
# This is the array manipulation (Reverse the array)
reversed_arr = np_array[::-1]
# Set the "text" parameter of "rotationMatrixValue_1_1_changed" object to the "changed" array value.
rotationMatrixValue_1_1_changed.configure(text="{}".format(reversed_arr))
# label for the slider
slider_label_helical_x = ttk.Label(root, text="Helical X:")
slider_label_helical_y = ttk.Label(root, text="Helical Y:")
slider_label_helical_z = ttk.Label(root, text="Helical Z:")
slider_label_helical_x.grid(column=0, row=0, sticky="w")
slider_label_helical_y.grid(column=0, row=1, sticky="w")
slider_label_helical_z.grid(column=0, row=2, sticky="w")
# slider
slider_x = ttk.Scale(
root, from_=-180, to=180, orient="horizontal", command=slider_changed, variable=current_value_x
)
slider_y = ttk.Scale(
root, from_=-180, to=180, orient="horizontal", command=slider_changed, variable=current_value_y
)
slider_z = ttk.Scale(
root, from_=-180, to=180, orient="horizontal", command=slider_changed, variable=current_value_z
)
slider_x.grid(column=1, row=0, sticky="we")
slider_y.grid(column=1, row=1, sticky="we")
slider_z.grid(column=1, row=2, sticky="we")
# Helical value label
value_label_x = ttk.Label(root, text=get_current_value()[0])
value_label_x.grid(column=2, row=0, sticky="n")
value_label_y = ttk.Label(root, text=get_current_value()[1])
value_label_y.grid(column=2, row=1, sticky="n")
value_label_z = ttk.Label(root, text=get_current_value()[2])
value_label_z.grid(column=2, row=2, sticky="n")
# Rotation Matrix Labels
rotationMatrixLabelHeaderOriginal = ttk.Label(root, text="Original rotation Matrix")
rotationMatrixValue_1_1_orig = ttk.Label(root)
rotationMatrixLabelHeaderChanged = ttk.Label(root, text="Changed rotation Matrix (Reversed)")
rotationMatrixValue_1_1_changed = ttk.Label(root)
rotationMatrixLabelHeaderOriginal.grid(column=0, row=3, sticky="w")
rotationMatrixValue_1_1_orig.grid(column=0, row=4, sticky="w")
rotationMatrixLabelHeaderChanged.grid(column=0, row=5, sticky="w")
rotationMatrixValue_1_1_changed.grid(column=0, row=6, sticky="w")
root.mainloop()
圖形用戶界面:

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/392896.html
上一篇:在Python中將串列組合成串列
