PythonでTkinterを使って簡単なGUIプログラムを作りたいと思っています。 私はwinPythonをダウンロードし、そのpython3.5.2
ディレクトリを私のpython環境として抽出しました。Pythonで区切られたクラスからTkinterウィジェット属性にアクセスしたり設定するにはどうしたらいいですか?
this threadで提供されたSpecTcl GUI builderを使用しました。 私はこのビルダーで単純なGUIを構築しましたが、GUIにはボタンとテキスト領域しかありません。ビルダーは2つのpythonファイルを生成します.1つは「SimpleGUI_ui.py」で、もう1つは「SimpleGUI.py」です。私はファイルをpython3.5.2
ディレクトリに置き、python3.5.2.exe SimpleGUI.py
を実行してGUIを表示します。
以下はSimpleGUI.py
コードです。それほど重要ではないいくつかのコードを省略しました。以下は
""" simpleGUI.py --
UI generated by GUI Builder Build 146 on 2016-12-05 22:47:05 from:
C:/Users/User/Downloads/guimaker/simpleGUI.ui
This file is auto-generated. Only the code within
'# BEGIN USER CODE (global|class)'
'# END USER CODE (global|class)'
and code inside the callback subroutines will be round-tripped.
The 'main' function is reserved.
"""
from tkinter import *
from simpleGUI_ui import SimpleGUIcd
# BEGIN USER CODE global
# END USER CODE global
class CustomSimpleGUI(SimpleGUI):
pass
# BEGIN CALLBACK CODE
# ONLY EDIT CODE INSIDE THE def FUNCTIONS.
# _button_1_command --
# Callback to handle _button_1 widget option -command
def _button_1_command(self, *args):
SimpleGUI._text_1.config(text="hello world") # This is useless
# _text_1_xscrollcommand --
# Callback to handle _text_1 widget option -xscrollcommand
def _text_1_xscrollcommand(self, *args):
pass
# I omit another yScrollCommand here.
def main():
# Standalone Code Initialization, DO NOT EDIT
try: userinit()
except NameError: pass
root = Tk()
demo = CustomSimpleGUI(root)
root.title('simpleGUI')
try: run()
except NameError: pass
root.protocol('WM_DELETE_WINDOW', root.quit)
root.mainloop()
if __name__ == '__main__': main()
SimpleGUI_ui.py
コードです:
""" simpleGUI_ui.py --
UI generated by GUI Builder Build 146 on 2016-12-05 22:47:05 from:
C:/Users/User/Downloads/guimaker/simpleGUI.ui
THIS IS AN AUTOGENERATED FILE AND SHOULD NOT BE EDITED.
The associated callback file should be modified instead.
"""
import tkinter
import os # needed for relative image paths
class SimpleGUI(object):
_images = [] # Holds image refs to prevent GC
def __init__(self, root):
# Widget Initialization
self._button_1 = tkinter.Button(root,
text = "_button_1",
)
self._text_1 = tkinter.Text(root,
height = 0,
width = 0,
)
# widget commands
self._button_1.configure(
command = self._button_1_command
)
self._text_1.configure(
xscrollcommand = self._text_1_xscrollcommand
)
self._text_1.configure(
yscrollcommand = self._text_1_yscrollcommand
)
# Geometry Management
self._button_1.grid(
in_ = root,
column = 1,
row = 1,
columnspan = 1,
ipadx = 0,
ipady = 0,
padx = 0,
pady = 0,
rowspan = 1,
sticky = ""
)
self._text_1.grid(
in_ = root,
column = 1,
row = 2,
columnspan = 1,
ipadx = 0,
ipady = 0,
padx = 0,
pady = 0,
rowspan = 1,
sticky = "news"
)
# Resize Behavior
root.grid_rowconfigure(1, weight = 0, minsize = 40, pad = 0)
root.grid_rowconfigure(2, weight = 0, minsize = 40, pad = 0)
root.grid_columnconfigure(1, weight = 0, minsize = 40, pad = 0)
root.grid_columnconfigure(2, weight = 0, minsize = 40, pad = 0)
だから私はそのコメントを踏襲:コードを_uiコールバックを変更ではなく。
私が遭遇した問題は、単に_text_1
ウィジェット内のテキストを変更することができないということです。私は、コールSimpleGUI._text_1.config(text="hello world")
を試してみましたが、私はGUI上のボタンをクリックしたとき、それは私に、以下の例外を与えた:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\User\Downloads\python-3.5.2\lib\tkinter\__init__.py", line 1550, in __call__
return self.func(*args)
File "simpleGUI.py", line 29, in _button_1_command
SimpleGUI._text_1.config(text="hello world")
AttributeError: type object 'SimpleGUI' has no attribute '_text_1'
誰もが、私は_text_1
ウィジェットを受け入れ、コールバック関数内のテキストを設定することができるか、私を助けていただけませんので、私は、私のGUIをインタラクティブにすることができました。うまくいけば、同じ方法で&にアクセスして、GUI上の他のウィジェットを制御することができます。ありがとう!
アンダースコアで始まる識別子はプライベートであり、クラス外ではアクセスできません。必要に応じて変更するメソッドを作成し、これらのメソッドを他のクラスから呼び出す必要があります。 – DyZ
こんにちは@DYZあなたの返事をありがとう、 'SimpleGUI_ui.py'の変更なしで、GUIウィジェットにアクセスする方法はありませんか?コードのコメントでは、「SimpleGUI_ui.py」は編集しないようにしています。 –