0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Tkinterでコンボボックスの値を変更する

Last updated at Posted at 2020-10-19

ソースコードは以下になります。

import tkinter
from tkinter import ttk

colors = ['Red', 'Green', 'Blue']


def btn_click():
    cb_colors.current(2)
    # cb_colors.set("Green")

    # print(cb_colors.get(), cb_colors.current())


root = tkinter.Tk()
root.geometry("200x200")
root.title("Color Picker")

cb_colors = ttk.Combobox(root, values=colors, width=10, state='readonly')
cb_colors.current(0)
cb_colors.place(x=50, y=50)

btn = tkinter.Button(root, text='ボタン', command=btn_click)
btn.place(x=50, y=100)

root.mainloop()

cb_colors.current(2)のようにインデックスを指定して値を設定してもよいし、cb_colors.set("Green")のように最初から値を指定してもOKです。
どちらの方法でも.get()で取得できる値と.current()で取得できるインデックスの値が変更されます。

最後まで読んでいただきありがとうございました。
またお会いしましょう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?