【问题标题】:tkinter grid identification is not working, cannot identify each pixel [duplicate]tkinter网格识别不起作用,无法识别每个像素[重复]
【发布时间】:2020-08-11 12:38:03
【问题描述】:

我知道这是一个非常个别的问题,但我认为这是 Tkinter 中的一个问题,因为我经常遇到类似的问题,我可以解决这些问题,并且答案也可能对其他人有益。

脚本是:

from tkinter import *
import random

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)
          # 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)

具体问题是,当您单击网格上的按钮时,它没有给出正确的“坐标”,而是给出了最后一个按钮。

【问题讨论】:

    标签: python-3.x tkinter


    【解决方案1】:

    我认为你覆盖了 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)
    

    使用这个小改动,您的程序就可以正常工作了。

    【讨论】:

    • 整体变化只有一行左右吗?
    • 变化的是类CMD的定义和按钮的定义。按钮通过调用CMD() 获得了添加的参数command。 (.config() 行变得多余)。
    • 其实我想知道你是否可以减少整个代码,以免混淆OP
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 2021-12-22
    • 2018-07-30
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多