Tkinter,如何从滑块中提取值、操作它们并将它们显示在我的 tk window 上的新网格中?
Tkinter, How do I extract values from a slider, manipulate them and display them in a new grid on my tk window?
我已经成功创建了 3 个滑块。每个滑块控制 X、Y 和 Z 螺旋角。我想将滑块的三个角度组合成一个 np.array,操纵该数组并在打开的新网格中显示结果 window。我希望结果在滑块移动时实时更新。
我可以操纵滑块,但我不知道该怎么做 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 行中,我试图操纵第一个滑块的值并显示它。目前,我什至无法显示该滑块的值,更不用说操作它了。
我已经根据您的原始代码编写了一个可行的解决方案。下面的代码实时显示了“原始”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()
界面:
我已经成功创建了 3 个滑块。每个滑块控制 X、Y 和 Z 螺旋角。我想将滑块的三个角度组合成一个 np.array,操纵该数组并在打开的新网格中显示结果 window。我希望结果在滑块移动时实时更新。 我可以操纵滑块,但我不知道该怎么做 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 行中,我试图操纵第一个滑块的值并显示它。目前,我什至无法显示该滑块的值,更不用说操作它了。
我已经根据您的原始代码编写了一个可行的解决方案。下面的代码实时显示了“原始”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()
界面: