Python tkinterによる簡単なGUIプログラミング
14511 ワード
本文はブロガーのオリジナル文章で、ブロガーの許可を得ずに転載してはならない.https://blog.csdn.net/Changcheng2017/article/details/82415703#commentBox
1.フォームおよびフォームの共通属性の作成
2.コントロールの一般的なレイアウトtkinterには、-pack()-grid()-place()の3つのレイアウトが同じフォームで混在して使用できない3つのレイアウトが用意されています. pack()は比較的簡単なレイアウトで、デフォルトでは上から下へレイアウトされ、一般的な参照フォーマットは: です.
-grid()は比較的簡単なレイアウトで、デフォルトは上から下へレイアウトされ、一般的な参照フォーマットは次のとおりです.
-place()は、コントロールの左上隅座標を直接指定することでコントロールを配置しますが、異なる解像度ではずれが発生する可能性があります.一般的な参照形式は次のとおりです.
3.コントロール-Label
4.コントロール-Button Buttonの命令はLabelと似ていますが、command=関数名が1つ増えただけです.このコマンドは通常、次のようなカスタム関数と組み合わせて使用できます.
5.コントロール-Entry Entry()は、単行テキストボックスを作成するために使用されます.Entry()ではtext属性が無効であり、Entry()で事前にテキストを入力するにはtextvariableを使用する必要があります.Entry()に入力したい文字がカスタマイズされている場合、パスワードを入力するときの暗証番号などはshow="で規定できます.Entry()で入力した値を変数に割り当てるには、次のようにします.
6.コントロール-Frame Frameの役割は、長方形の形をした親クラスを作成することであり、コンテナとして他のコントロールを収容し、異なる種類のコントロール間のグループ化を実現するために多く使用される.
7.コントロール-Checkbutton Checkbuttonは1つの陳述に対してYesまたはNoの選択を行うために使用され、基本的な使い方は上記と類似しているが、チェック動作に対する命令が追加されている. command=,指定チェックして実行する関数 onvalue=,checkbuttonがチェックされている場合の値を指定し,通常は1 をとる. offvalue=、checkbuttonが非チェック状態にある場合の値を指定します.通常は0 をとります. variable=、checkbutton状態を追跡する変数を指定します.次に、checkbuttonでカスタム関数を実行する例を示します.
他にもいくつかのツールがあります.
8.コントロール-Radiobutton Radiobuttonは、checkbuttonと同様に使用される複数選択を実行するためのコントロールですが、各オプションにトリガ後のアクションを指定するには、value=各checkを追跡するために通常commandカスタム関数を使用してトリガする必要があります.
9.コントロール-Combox Combox簡単なドロップダウンリストを作成します.このメニューにはlabelがなく、メニューのオプションにアクションを設定できます.通常、イベントにバインドされて使用されます.
10.コントロール-Menu Menuは成熟したドロップダウンメニューで、このメニューにラベルを指定できます.Menuインスタンスを作成することでName.add_commandとtop.config(menu=Name)を組み合わせると、最上位にドロップダウンメニューを作成できます.commandを使用して、各オプションを選択したアクションを指定したり、イベントをバインドして関数を実行したりできます.menuはレイアウト操作を必要としません.
1.フォームおよびフォームの共通属性の作成
import tkinter # tkinter
Window_Root = tkinter.Tk() #
# :
Window_Root.title('Hello tkinter!') #
Window_Root.geometry('300x200') # , x
Window_Root.resizable(width=False, height=True) # 、 ,true ,false
'''
'''
Window_Root.mainloop() #
2.コントロールの一般的なレイアウトtkinterには、-pack()-grid()-place()の3つのレイアウトが同じフォームで混在して使用できない3つのレイアウトが用意されています.
Window_Root.Widget_1.pack(fill = , # :X( )、Y( )、BOTH( )、NONE( , )
side = '' , # :left、right、top、bottom
expand = '', # yes/no fill BOTH, 。
anchor = '') # , :N、E、S、W、NW、NE、SW、SE、CENTER( CENTER)
-grid()は比較的簡単なレイアウトで、デフォルトは上から下へレイアウトされ、一般的な参照フォーマットは次のとおりです.
Window_Root.Widget_1.grid(row= , #
column = , #
sticky = '', #
rowspan = , #
columnspan = ) #
'''
gird() , :
colours = ['red', 'green', 'orange', 'white']
r = 0
for c in colours:
tkinter.Label(text=c, relief='ridge', width=20).grid(row=r, column=0)
r = r + 1
-place()は、コントロールの左上隅座標を直接指定することでコントロールを配置しますが、異なる解像度ではずれが発生する可能性があります.一般的な参照形式は次のとおりです.
Window_Root.Widget_1.place(x = , # x ( )
y = , # y ( )
relx = , # x
rely = ) # y
### ,
3.コントロール-Label
Label_1 = tkinter.label(master = , # master
###
test = '', # label ,
textvariable = , # label ,
'''
:
VAR = bla,bla,bla #
str_obj_1 = tkinter.StringVar()
str_obj_1.set(f'{VAR}') # VAR ''
textvariable = str_obj_1
'''
font = '', # ,
justify = '', # :center( ),left,right
foreground = '', # , , white,red, RGB
anchor = '', # :n,s,w,e,ne,nw,sw,se,center
###
height = , # label
weight = , # label
padx = , # label ,int
pady = , # label ,int
background = '', # label , , white,red, RGB
###
relief = '', # :flat( ),sunken,raised,groove,ridge, , 0
borderwidth = , # label
### , gif,ppm/pgm bmp
image = image_1, #
compound = "", # :None , ;bottom/top/left/right, / / / ;center,
'''
:
image_1 = TKinter.PhotoImage(file = "gif 、ppm/pgm ")
image_1 = TKinter.BitmapImage(file = "bmp ")
'''
###
state = '', # , normal, :activite,disable
cursor = '', # :arrow、circle、clock、cross、dotbox、exchange、fleur、heart、heart、man、mouse、pirate、plus、shuttle、sizing、spider、spraycan、target、star、tcross、trek、watch
4.コントロール-Button Buttonの命令はLabelと似ていますが、command=関数名が1つ増えただけです.このコマンドは通常、次のようなカスタム関数と組み合わせて使用できます.
def hello():
print('hello button')
Window_Root = tkinter.Tk()
Button_1 = tkinter.Button(master=Window_Root, text='Print Hello Button', command=hello).pack()
Window_Root.mainloop()
5.コントロール-Entry Entry()は、単行テキストボックスを作成するために使用されます.Entry()ではtext属性が無効であり、Entry()で事前にテキストを入力するにはtextvariableを使用する必要があります.Entry()に入力したい文字がカスタマイズされている場合、パスワードを入力するときの暗証番号などはshow="で規定できます.Entry()で入力した値を変数に割り当てるには、次のようにします.
v = tkinter.StringVar() # v
entry_1 = tkinter.Entry(Window_Root, textvariable=v) # Entry() v
value = v.get() # v value
6.コントロール-Frame Frameの役割は、長方形の形をした親クラスを作成することであり、コンテナとして他のコントロールを収容し、異なる種類のコントロール間のグループ化を実現するために多く使用される.
frame_1 = Frame(master=, OPTIONS,。。。) # Frame
7.コントロール-Checkbutton Checkbuttonは1つの陳述に対してYesまたはNoの選択を行うために使用され、基本的な使い方は上記と類似しているが、チェック動作に対する命令が追加されている.
def act_selection:
if var_1.get() == 1:
label_1.config(text = 'WOW!!!!!!!Me tooooooooo!')
else:
label_1.config(text = 'Too bad, you shall lose a good guy....')
var_1 = tkinter.IntVar()
check_iflikeme = tkinter.Checkbutton(master=Window, text='Do you like me?',\
variable=var_1, onvalue=1, offvalue=0,\
command=act_selection).pack()
window.mainloop()
他にもいくつかのツールがあります.
check_button.select() #
check_button.deselect() #
8.コントロール-Radiobutton Radiobuttonは、checkbuttonと同様に使用される複数選択を実行するためのコントロールですが、各オプションにトリガ後のアクションを指定するには、value=各checkを追跡するために通常commandカスタム関数を使用してトリガする必要があります.
### , for :
text_RaB = ['Red', 'Blue', 'yellow']
index_RaB = [1, 2, 3, 4]
def print_color:
if value == 1:
print(text_RaB[0])
elseif value == 2:
print(text_Rab[1])
elseif value == 3:
print(text_Rab[2])
else :
print(text_Rab[3])
for color, index in text_RaB, index_RaB:
Window_Radiobutton = tkinter.Radiobutton(master=None,text=color, value=index, command=print_color).pack()
9.コントロール-Combox Combox簡単なドロップダウンリストを作成します.このメニューにはlabelがなく、メニューのオプションにアクションを設定できます.通常、イベントにバインドされて使用されます.
# , *arg
def show_msg(*arg) :
print('Your favorite fruit is:', Windows_Combobox.get()) # Windows_Combobox.get()
# Combobox :
fruits_name = tkinter.StringVar() # , Combobox
Windows_Combobox = tkinter.Combobox(master=Window_Root, textvariable=fruits_name) # Combobox, fruits_name
Windows_Combobox['values'] = ['Apple', 'Banana', 'Pitch'] # ,
Windows_Combobox.bind("<>" , show_msg) # , show_msg : , show_msg
Windows_Combobox.pack()
10.コントロール-Menu Menuは成熟したドロップダウンメニューで、このメニューにラベルを指定できます.Menuインスタンスを作成することでName.add_commandとtop.config(menu=Name)を組み合わせると、最上位にドロップダウンメニューを作成できます.commandを使用して、各オプションを選択したアクションを指定したり、イベントをバインドして関数を実行したりできます.menuはレイアウト操作を必要としません.
def hello():
if value == 1:
print()
elif value == 2:
print()
else:
print()
Index = (1, 2, 3)
Item = ('Apple', 'Banana', 'Pitch')
menubar = tkinter.Menu(master) # master
for i, name in Index, Item # label
menubar.add_command(label=name, value=i)
master.config(menu=menubar) # master menu
menubar.bind("<>" , hello) #