選択したラジオボタンの値を取得できません。現在、ラジオボタンが使用する私の変数に.get()
関数を使用すると、何も返されません。 .get()
関数なしでメッセージボックスを使用すると、PY_VAR
xが返されます。ここで、xはtkinter変数の特定のインスタンスです。Tkinterにアクセスする際の問題Radio Button CTRL var
ラジオボタンは変数を正しく使用していますが、一度にラジオボタンを1つだけ選択できますが、値を正しく読み取ることができません。私は現在の選択を読んでそれを別の変数に入れたいと思っています。
以下は省略されたコードです(プログラムのラジオボタンの部分のみが表示されています)。
from tkinter import *
from tkinter import messagebox, ttk
class init_system:
def __init__(self):
self.init_vars = {"w_audiocodec": "Windows Media Audio 9.2 Lossless"}
class render_window:
def __init__(self, height, width, window_title):
self.root_window = Tk()
w = width
h = height
ws = self.root_window.winfo_screenwidth() # width of the screen
hs = self.root_window.winfo_screenheight() # height of the screen
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)
self.root_window.title(window_title)
self.root_window.minsize(width, height)
self.root_window.geometry('%dx%d+%d+%d' % (w, h, x, y))
self.master_dictionary = {"radio_ctrl": StringVar()}
def new_button(self, button_text, button_command="", grid_row=0, grid_column=0, grid_sticky="NESW", grid_columnspan=1, grid_rowspan=1):
self.button = ttk.Button(self.root_window, text=button_text, command=button_command)
self.button.grid(row=grid_row, column=grid_column, sticky=grid_sticky, columnspan=grid_columnspan, rowspan=grid_rowspan)
self.responsive_grid(grid_row, grid_column)
def new_label(self, label_text, text_alignment="center", grid_row=0, grid_column=0, grid_sticky="NESW", grid_columnspan=1, grid_rowspan=1):
self.label = ttk.Label(self.root_window, text=label_text, anchor=text_alignment)
self.label.grid(row=grid_row, column=grid_column, sticky=grid_sticky, columnspan=grid_columnspan, rowspan=grid_rowspan)
self.responsive_grid(grid_row, grid_column)
def responsive_grid(self, row_responsive=0, column_responsive=0, row_weight_num=1, column_weight_num=1):
self.root_window.grid_columnconfigure(column_responsive, weight=column_weight_num)
self.root_window.grid_rowconfigure(row_responsive, weight=row_weight_num)
def new_radio_button(self, widget_text="Radio Button", radio_value="Radio Btn", radio_command="", grid_row=0, grid_column=0, grid_sticky="NESW", grid_columnspan=1, grid_rowspan=1):
self.radio_button = ttk.Radiobutton(self.root_window, text=widget_text, variable=self.master_dictionary["radio_ctrl"], value=radio_value, command=radio_command)
self.radio_button.grid(row=grid_row, column=grid_column, sticky=grid_sticky, columnspan=grid_columnspan, rowspan=grid_rowspan)
self.responsive_grid(grid_row, grid_column)
class change_var_window(render_window):
change_var_window_values = {"example_data_below": "Check it out!",
"var_to_change": "show_ui",
"toggle": False,
"radio": False,
"free_form":False,
"line_one": "Current value of:",
"line_two": "some varible name here passwed with a dicrionary",
"Custom_Data_Bool": False,
"Custom_Disable": "Enable me",
"Custom_Enable": "Disable me",
"radio_list": [("Radio Button 1", "btn_1"), ("Radio Button 2", "btn_2"), ("Radio Button 3", "btn_3")],
"is_number": True}
def save_radio_var(self):
vars_system.init_vars[self.change_var_window_values["var_to_change"]] = self.master_dictionary["radio_ctrl"].get()
messagebox.showinfo("debug", self.master_dictionary["radio_ctrl"].get())
def create_change_var_window(self):
self.new_label(self.change_var_window_values["line_one"], grid_columnspan=2)
self.new_label(self.change_var_window_values["line_two"], grid_row=1, grid_columnspan=2)
if self.change_var_window_values["radio"]:
grid_placement = 2
self.master_dictionary["radio_ctrl"] = StringVar()
#self.master_dictionary["radio_ctrl"].set(vars_system.init_vars[self.change_var_window_values["var_to_change"]])
for radio_name, value in self.change_var_window_values["radio_list"]:
self.new_radio_button(widget_text=radio_name, radio_value=value, grid_row=grid_placement)
grid_placement +=1
grid_placement -=1
self.new_button("Cancle", self.root_window.destroy, grid_row=grid_placement, grid_column=1)
grid_placement -=1
self.new_button("Save", self.save_radio_var, grid_row=grid_placement, grid_column=1)
# Radio Requires:
# "radio_list" "var_to_change"
self.root_window.mainloop()
def radio_example():
radio = change_var_window(300, 350, "Radio Select")
radio.change_var_window_values.update({"free_form": False, "toggle": False, "radio": True, "var_to_change": "w_audiocodec", "line_one": "Current value of w_audiocodec:", "line_two": vars_system.init_vars["w_audiocodec"]})
radio.change_var_window_values.update({"radio_list": [("Windows Media Audio 9.2", "Windows Media Audio 9.2"), ("Windows Media Audio 9.2 Lossless", "Windows Media Audio 9.2 Lossless"), ("Windows Media Audio 10 Professional", "Windows Media Audio 10 Professional")]})
#main_window.root_window.withdraw()
radio.create_change_var_window()
vars_system = init_system()
main_window = render_window(200, 250, "Main Window")
main_window.new_button("Radio Var", radio_example)
main_window.root_window.mainloop()
問題は、複数のルートウィンドウを作成しているという事実に関係している
注:PEP8に従えば、あなたのコードは非常に理解しやすくなります。具体的には、クラス名は大文字で始まる必要があります。 –
大丈夫ですが、私はこのコードを完成させることを念頭に置いています。メインプログラムにプッシュすると、pep8に従うように更新します。 –