Python tkinterのCommboboxの使用説明


1、Compboxの基礎的な属性

# -*- encoding=utf-8 -*-
import tkinter
from tkinter import *
from tkinter import ttk

if __name__ == '__main__':
  win = tkinter.Tk() #   
  win.title('     ') #   
  screenwidth = win.winfo_screenwidth() #     
  screenheight = win.winfo_screenheight() #     
  width = 600
  height = 500
  x = int((screenwidth - width) / 2)
  y = int((screenheight - height) / 2)
  win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) #       
  value = StringVar()
  value.set('CCC')
  values = ['AAA', 'BBB', 'CCC', 'DDD']
  combobox = ttk.Combobox(
      master=win, #    
      height=10, #   ,         
      width=20, #   
      state='readonly', #      normal(     )、readonly(   )、 disabled
      cursor='arrow', #         arrow, circle, cross, plus...
      font=('', 20), #   
      textvariable=value, #   StringVar       
      values=values, #         
      )
  print(combobox.keys()) #          
  combobox.pack()
  win.mainloop()

2、選択したイベントをバインドする

# -*- encoding=utf-8 -*-
import tkinter
from tkinter import *
from tkinter import ttk


def choose(event):
  #     
  print('     :{}'.format(combobox.get()))
  print('value  :{}'.format(value.get()))


if __name__ == '__main__':
  win = tkinter.Tk() #   
  win.title('     ') #   
  screenwidth = win.winfo_screenwidth() #     
  screenheight = win.winfo_screenheight() #     
  width = 600
  height = 500
  x = int((screenwidth - width) / 2)
  y = int((screenheight - height) / 2)
  win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) #       
  value = StringVar()
  value.set('CCC') #     CCC==combobox.current(2)

  values = ['AAA', 'BBB', 'CCC', 'DDD']
  combobox = ttk.Combobox(
      master=win, #    
      height=10, #   ,         
      width=20, #   
      state='normal', #      normal(     )、readonly(   )、 disabled
      cursor='arrow', #         arrow, circle, cross, plus...
      font=('', 20), #   
      textvariable=value, #   StringVar       
      values=values, #         
      )
  combobox.bind('<<ComboboxSelected>>', choose)
  print(combobox.keys()) #          
  combobox.pack()
  win.mainloop()

以上がPython tkinterのCommboboxの使用概要の詳細です。Python tkinterのCompboxの使用に関する資料は他の関連記事に注目してください。