亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

tkinter 網格識別不起作用,無法識別每個像素

tkinter 網格識別不起作用,無法識別每個像素

慕姐8265434 2023-05-16 16:43:58
我知道這是一個非常個別的問題,但我認為這是 Tkinter 中的一個問題,因為我經常遇到類似的問題,我解決了這些問題,答案也可能對其他人有益。腳本是:from tkinter import *import randomclass gui:  def __init__(self):    win=self.win=Tk()    win.title('Ploters Data!')  def identifier(self,x,y):    print('id',x,y)       def createGraph(self,rows,columns):    for xrow in range(rows+1):      for ycolumn in range(columns+1):        if xrow == 0 or ycolumn == 0:          text = '--'          if xrow == 0:            if ycolumn==5:              text='5'            if ycolumn==10:              text='10'            if ycolumn == 0:            if xrow==5:              text='5'            if xrow==10:              text='10'          if xrow == ycolumn == 0:              text='[]'          pixel = Button(self.win,padx=10,pady=10,text=text)          # print('click',xrow,ycolumn)          pixel.config(command=lambda button=pixel: self.identifier(xrow,ycolumn))          pixel.grid(row=xrow,column=ycolumn)        else:          pixel = Button(self.win,padx=10,pady=10)          # print('click',xrow,ycolumn)          pixel.config(command=lambda button=pixel: self.identifier(xrow,ycolumn))          pixel.grid(row=xrow,column=ycolumn)        # print(xrow,ycolumn)    self.win.mainloop()s=gui()s.createGraph(15,10)具體問題是,當您單擊網格上的按鈕時,它不會給出正確的“坐標”,而是給出最后一個按鈕。
查看完整描述

1 回答

?
湖上湖

TA貢獻2003條經驗 獲得超2個贊

我想你覆蓋了 lambda 函數,它在這個例子中是可變的。我不知道為什么會發生這種情況(tkinter GUI 也有同樣的問題)但我現在知道一個簡單的解決方法。


如果您定義一個類,它接受一個函數和一系列參數和關鍵字參數,并且只使用這些參數調用該函數,您可以將它作為按鈕中的命令引用。使用此解決方法,您可以省略 lambda 并僅調用帶參數的函數:


from tkinter import *

import random



class CMD: #Auxilliary function for callbacks using parameters. Syntax: CMD(function, argument1, argument2, ...)

    def __init__(s1, func, *args):

        s1.func = func

        s1.args = args

    def __call__(s1, *args):

        args = s1.args+args

        s1.func(*args)


class gui:

  def __init__(self):

    win=self.win=Tk()

    win.title('Ploters Data!')

  def identifier(self,x,y):

    print('id',x,y) 

    

  def createGraph(self,rows,columns):

    for xrow in range(rows+1):

      for ycolumn in range(columns+1):

        if xrow == 0 or ycolumn == 0:

          text = '--'

          if xrow == 0:

            if ycolumn==5:

              text='5'

            if ycolumn==10:

              text='10'  

          if ycolumn == 0:

            if xrow==5:

              text='5'

            if xrow==10:

              text='10'

          if xrow == ycolumn == 0:

              text='[]'

          pixel = Button(self.win,padx=10,pady=10,text=text,command=CMD(self.identifier,xrow,ycolumn))

          pixel.grid(row=xrow,column=ycolumn)

        else:

          pixel = Button(self.win,padx=10,pady=10,command=CMD(self.identifier,xrow,ycolumn))

          pixel.grid(row=xrow,column=ycolumn)

        # print(xrow,ycolumn)

    self.win.mainloop()


s=gui()

s.createGraph(15,10)

使用這個小改動,您的程序可以正常工作。


查看完整回答
反對 回復 2023-05-16
  • 1 回答
  • 0 關注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號