前提(質問の内容)
BlenderとPythonで対話型の自動車生成システムを作っています。
現在対話をするためのGUIをPythonで開発しています。
TkinterライブラリのCheckButtonを使用し、テスト用のアンケート機能を実装しようと試みたのですが、うまくいきませんでした。
実現したいこと
ここに実現したいことを箇条書きで書いてください。
- TkinterのCheckボタンを使用し、チェック状態(変数の値:cool_imgやcute_img)を他のスクリプトにも流用できるようにしたい。
発生している問題・エラーメッセージ
自動車を作成をクリック
→チェックボタンを押した後、チェック状態をprintをクリック
→ターミナルでチェックボタンの状態がprintされるはずが、状態が反映されない。
2つのチェックボタンの状態に関わらず常に
coolがチェックされています cuteにチェックがされていません
とターミナルに表示される
該当のソースコード
my_window.py
python
1#カレントディレクトリを取得・確認 2import os 3# tkinterのインポート 4import tkinter as tk 5#別ファイルのインポート 6import image_select_window 7 8def quit_now_func(): 9 root.quit() 10#PATHの読み込みと設定 11def Path_setting(): 12 path = os.getcwd() 13 print(path) 14 15# 終了確認 16def quit_yn(): 17 if tk.messagebox.askyesno("確認", "終了しますか?"): 18 root.destroy() 19 20##### GUI ##### 21# rootウインドウの作成 22root = tk.Tk() 23 24# rootウィンドウの名前 25root.title("メイン画面") 26# rootウインドウのサイズ指定 27root.state("zoomed") 28 29# メニューバーの作成 30menubar = tk.Menu(root) 31root.configure(menu = menubar) 32 33# ファイルメニュー 34filemenu = tk.Menu(menubar, tearoff = 0) 35menubar.add_cascade(label = "File", menu = filemenu) 36 37# ~内容 38filemenu.add_command(label = "Open File...") 39 40# セパレーター 41filemenu.add_separator() 42filemenu.add_command(label = "Exit", command = lambda: root.destroy()) 43 44#タイトルラベル 45title_label = tk.Label(root, text='自動車生成アプリ', font=("", 80)) 46title_label.place(relx=0.25,rely=0.1,relwidth=0.5,relheight=0.11) 47 48#作成 49genetic_window_button = tk.Button(root, text = "自動車を作成する", command = image_select_window.window, font=("メイリオ", 50), bg="skyblue") 50genetic_window_button.place(relx=0.35,rely=0.3,relwidth=0.3,relheight=0.1) 51 52#バージョン 53version_label = tk.Label(root, text='Ver.0.1', font=("", 40)) 54version_label.place(relx=0.4,rely=0.8,relwidth=0.2,relheight=0.1) 55 56#pathの設定 57path_button = tk.Button(root, text = "pathを確認する", command = Path_setting, font=("", 40)) 58path_button.place(relx=0.4,rely=0.5,relwidth=0.2,relheight=0.1) 59 60 61# exitボタン 62exit_button = tk.Button(root, text="システムを終了", command=quit_yn, font=("", 40)) 63exit_button.place(relx=0.4,rely=0.7,relwidth=0.2,relheight=0.1) 64 65quit_button = tk.Button(root, text = "強制終了(2回クリック)", command = quit_now_func, font=("メイリオ", 10), bg="red") 66quit_button.place(relx=0.01,rely=0.9,relwidth=0.1,relheight=0.05) 67 68# ウインドウ状態の維持 69root.mainloop()
image_select_window.py
python
1import tkinter as tk 2from tkinter import * 3import tkinter.ttk as ttk 4import subprocess 5#別ファイルのインポート 6 7def window(): 8 img_win = tk.Tk() 9 img_win.title("選択画面") 10 w = img_win.winfo_screenwidth() 11 # パソコン画面の高さを取得 12 h = img_win.winfo_screenheight() 13 img_win.geometry(str(w)+"x"+str(h)+"+0"+"+0") 14 #ウィンドウを閉じる 15 def quit_yn(): 16 img_win.destroy() 17 18 def quit_now_func(): 19 img_win.quit() 20 21 # Styleの指定 22 style = ttk.Style() 23 style.configure("blue.TSeparator", background="blue") 24 25 statusbar = tk.Label(img_win, text = "作成したい自動車のイメージを選択してください", font=("", 40), bd = 1, anchor = tk.N) 26 statusbar.place(relx=0.1,rely=0.05,relwidth=0.8,relheight=0.1) 27 28 cool_var = tk.BooleanVar() 29 cute = tk.BooleanVar() 30 cool_var.set(TRUE) 31 def image_check(): 32 if cool_var.get(): 33 cool_img = 1 34 print("coolがチェックされています") 35 else: 36 cool_img = 0 37 print("coolがチェックされていません") 38 if cute.get(): 39 cute_img = 1 40 print("cuteがチェックされています\n") 41 else: 42 cute_img = 0 43 print("cuteにチェックがされていません\n") 44 45 cool_label = tk.Label(img_win, textvariable=cool_var, text='かっこいい') 46 cool_label.place(relx=0.01,rely=0.3,relwidth=0.2,relheight=0.1) 47 cute_label = tk.Label(img_win, textvariable=cute, text='かわいい') 48 cute_label.place(relx=0.50,rely=0.3,relwidth=0.2,relheight=0.1) 49 50 51 select_cool_button = tk.Checkbutton(img_win, text='かっこいい', font=("", 30), variable=cool_var) 52 select_cool_button.place(relx=0.01,rely=0.1,relwidth=0.3,relheight=0.1) 53 select_cute_button = tk.Checkbutton(img_win, text='かわいい', font=("", 30), variable=cute) 54 select_cute_button.place(relx=0.50,rely=0.1,relwidth=0.3,relheight=0.1) 55 56 # Nextボタン 57 #exit_button = tk.Button(img_win, text="メイン画面に戻る", command=quit_yn, font=("", 50), bg="orange") 58 #exit_button.place(relx=0.01,rely=0.4,relwidth=0.49,relheight=0.1) 59 60 roading_window_button = tk.Button(img_win, text = "チェック状態をprint", command = image_check, font=("メイリオ", 30), bg="skyblue") 61 roading_window_button.place(relx=0.01,rely=0.6,relwidth=0.49,relheight=0.1) 62 63 quit_button = tk.Button(img_win, text = "強制終了(2回クリック)", command = quit_now_func, font=("メイリオ", 10), bg="red") 64 quit_button.place(relx=0.01,rely=0.9,relwidth=0.1,relheight=0.05) 65 66 img_win.mainloop()
実行方法
$ python3 my_window.py
補足情報(FW/ツールのバージョンなど)
Pythonのバージョン:Python 3.10.4

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2023/01/17 07:01