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?

備忘録(Python)

Last updated at Posted at 2024-04-20

ボタンが上手くいかない!

ボタンを押して、入力してもらった値を適当な変数に保存しときたい
getしたやつはstrだからint(ボタン.get())したいのに

ValueError: invalid literal for int() with base 10: ''

valueErrorになる。

引数を使うときはlambdaを入れよう!

use_lambda.py
def ButtonClick(Num):
  a = edit_box1.get()
  print(a)
  Num = int(a)

root = tk.Tk()

root.geometry = ("600*300")
root.title("ラーメン屋")
label = tk.Label (root,text="人数の入力")
label.place(x=20,y=20)
edit_box1 = tk.Entry(width = 20)
edit_box1.place(x = 50 , y = 40)

Num_people = 0
button1 = tk.Button(root,text = "チェック",command=lambda:ButtonClick(Num_people))
button1.place(x= 120,y=60)

button1を定義してるところで、command=のあとに
lambda: を入れるだけで解決した。

参考:
[Python] Tkinterのボタンが押された時に引数付き関数を呼び出したい。
(https://www.earthlink.co.jp/engineerblog/technology-engineerblog/7744/)

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?