

pythonが使えるanaconda を使い、High and low ゲームを作成しています。

tkinterで以下の通りに書いて実行したらエラーが表示されました。  
どうすればよいでしょうか。

**エラーメッセージ:**

[code]

    Exception in Tkinter callback
    Traceback (most recent call last):
      File "C:\ProgramData\anaconda3\Lib\tkinter\__init__.py", line 1948, in __call__
        return self.func(*args)
               ^^^^^^^^^^^^^^^^
    TypeError: chek_high() takes 0 positional arguments but 1 was given
    
[/code]

**現状のコード:**

[code]

    import tkinter as tk
    import random as ran
    mark = ['spade','diamond','heart','club']
    num  = range(1,14)
    cards= [(i,j)for i in mark for j in num]
    ran.shuffle(cards)
    print(cards)
    open_card   = cards[1][1]
    answer_card = cards[2][1]
    
    root = tk.Tk()
    
    
    #======================GUI=======================#
    
    root.title(u"High and low game")
    root.geometry("1000x750")
    lb = tk.Label(root,text=u"High and low game スタート!!",font = ('Arial',25))
    lb.grid(row=0)
    lb2 = tk.Label(root,text=u"現在のカードは、",font = ('Arial',20))
    lb2.grid(row=1)
    lb3 = tk.Label(root,text=open_card,font = ('Arial',300))
    lb3.grid(row=2)
    lb4 = tk.Label(root,text=u"Highかな❓❔ lowかな❓❔",font = ('Arial',20))
    lb4.grid(row=3)
    def chek_high():
        if open_card < answer_card:
            lb5 = tk.Label(root,text=u"お見事‼　正解‼",font = ('Arial',20))
            lb5.pack()
        elif open_card > answer_card:
            lb6 = tk.Label(root,text=u"残念　不正解…",font = ('Arial',25))
            lb6.pack()
        root.mainloop()
    high = tk.Button(root,text=u'High',width=10)
    high.bind("<Button-1>",chek_high)
    high.grid(column=0, row=4)
    low = tk.Button(root,text=u'low',width=10)
    low.grid(column=2, row=4)
    
    root.mainloop()
    
[/code]

[![この通りです](https://i.stack.imgur.com/LUrWm.png)](https://i.stack.imgur.com/LUrWm.png)


CC BY-SA 4.0
2023-08-15T00:32:33.870
95938
2023-08-15T05:19:41.713
2023-08-15T05:18:28.103
3060
59479
question
python anaconda
tkinterでbindした関数についてエラーがでます: TypeError: chek_high() takes 0 positional arguments but 1 was given


下記でひとまずエラーなく動作するようになるかと思います。

修正点は下記2点です。

  1. `def chek_high():` → `def chek_high(event):`  
[コールバック関数](https://office54.net/python/tkinter/tkinter-bind-event#section3)

  2. chek_high関数内のpack()をgrid()に変更（併用できないのでgridに統一）

ただし下記コードでは数字ぴったりの場合に何も表示されないのでその場合の対応を考える必要があるかと思います。

**修正後のコード:**

[code]

    import tkinter as tk
    import random as ran
    mark = ['spade','diamond','heart','club']
    num  = range(1,14)
    cards= [(i,j)for i in mark for j in num]
    ran.shuffle(cards)
    print(cards)
    open_card   = cards[1][1]
    answer_card = cards[2][1]
    
    root = tk.Tk()
    
    
    #======================GUI=======================#
    
    root.title(u"High and low game")
    root.geometry("1000x750")
    lb = tk.Label(root,text=u"High and low game スタート!!",font = ('Arial',25))
    lb.grid(row=0)
    lb2 = tk.Label(root,text=u"現在のカードは、",font = ('Arial',20))
    lb2.grid(row=1)
    lb3 = tk.Label(root,text=open_card,font = ('Arial',300))
    lb3.grid(row=2)
    lb4 = tk.Label(root,text=u"Highかな❓❔ lowかな❓❔",font = ('Arial',20))
    lb4.grid(row=3)
    def chek_high(event):
        if open_card < answer_card:
            lb5 = tk.Label(root,text=u"お見事‼　正解‼",font = ('Arial',20))
            # lb5.pack()
            lb5.grid(column=0, row=5)
        elif open_card > answer_card:
            lb6 = tk.Label(root,text=u"残念　不正解…",font = ('Arial',25))
            # lb6.pack()
            lb6.grid(column=0, row=5)
        root.mainloop()
    high = tk.Button(root,text=u'High',width=10)
    high.bind("<Button-1>",chek_high)
    high.grid(column=0, row=4)
    low = tk.Button(root,text=u'low',width=10)
    low.grid(column=2, row=4)
    
    lb5 = tk.Label(root)
    lb5.grid(column=0, row=5)
    
    root.mainloop()
    
[/code]


CC BY-SA 4.0
2023-08-15T02:03:42.220
95940
2023-08-15T05:19:41.713
2023-08-15T05:19:41.713
3060
39819
95938
answer