繁体   English   中英

TypeError 接受 2 个位置参数,但给出了 3 个

[英]TypeError takes 2 positional arguments but 3 were given

我正在开发一个程序,将图像转换为 .mcfunction 文件,用于我的世界基岩版和 java 版。 在我的程序中,我使用 for 循环在image.getpixel中插入 x 和 y 坐标。 然而,整个晚上我都被错误困住了

TypeError: getpixel() 接受 2 个位置参数,但给出了 3 个

并且不确定是什么原因造成的。 我以前没有这个问题,但我最近添加了一个旋转图像预览的功能,突然这开始发生了。 这是我的程序:

  from tkinter import *
  from tkinter.ttk import *
  from tkinter import filedialog
  from PIL import ImageTk,Image
  import os
  from casegen import casegenerator
  from tkinter import ttk
  from tkinter import messagebox

  #creates window
  root = Tk()
  root.title("Pixelcraft V1.0")
  root.iconbitmap("pixlcraftlogo_tKt_icon.ico")
  root.geometry("500x350")
  print("Sucessfully loaded window!")


  #declares a button style
  style = Style()
  style.configure('W.TButton', font =
                 ('calibri', 10, 'bold', 'underline'), 
                  foreground = 'black')

  #declares a canvas style
  canvas_width = 390
  canvas_height = 210

  my_canvas = Canvas(root, width=canvas_width, height=canvas_height, highlightthickness=1,                         
  highlightbackground="gray")
  my_canvas.configure(bg='white')

  my_canvas.place(x=50, y=70)

  startupimg = ImageTk.PhotoImage(Image.open("PixlCraftLogo.png"))

  #loads image and puts it on canvas
  canvasstartuplogo = my_canvas.create_image(195, 105,image=startupimg)

  #widgets and functions:

  #makes a prompt to name the function
  def filenameinput():
      convertbtn.destroy()
      ent.place(x=160, y=30)
      ent.insert(0, "Enter a filename")

      confbutton.place(x=300, y=28)

  #color comparer
  def warningmaker():
      messagebox.showinfo("Warning", "Depending on how large your image is, PixelCraft may become                               
  unresponsive. Please be patient and dont close the tab to prevent file corruption.")


      imgtofunc()

  def imgtofunc():

      confbutton.destroy()
      ent.destroy()
      fullcommand = str("")
      steps = width/100

      #scans the pixels in x axis
      for i in range(width):

    
          #scans the pixels in y axis
          for j in range(height):

              #returns rgb values as tuple
        
              mred, mgreen, mblue = imgfcalc.getpixel(i, j)
    
              blockofpix = casegenerator(mred, mgreen, mblue)

              singlecommand = "setblock" + " " + "~" + " " + "~" + str(j*-1) + " " + "~" + str(i) + " 
  " + str(blockofpix) + "\n"
        
              singlecommand = str(singlecommand)
        
              fullcommand = fullcommand + singlecommand
        
              #print(casegenerator(mred, mgreen, mblue))
                 
              #print(test1)

      print(fullcommand)

  #function executes after import button is pressed 
  def importimages():
      #global variables must be defined in order for images to work
      global userchosenimg
      global userfile
      global newtext
      global width
      global height
      global imgfcalc

      #file dialog prompt
      userfile = filedialog.askopenfilename(title="Select an image", filetypes = (("All Files", 
  "*.*"), ("jpeg", ".jpeg"), ("jpg", ".jpg"), ("png", ".png")))


      #creates a variable of just the file name rather than the whole location
      for i in range(len(userfile)):
          userfilestrindex = userfile[i*-1]
          if userfilestrindex == "/":
              charsuntilslash = len(userfile) - i
              newtext = userfile[charsuntilslash+1:len(userfile)]
              break          

      #displays name of selected image
      userimagefilename = Label(root, text=newtext, font=("Arial", 8))
      userimagefilename.place(x=40, y=30)


      #removes the import button
      importimages1.place_forget()

      #removes the default logo
      my_canvas.delete(canvasstartuplogo)

      #removes the name of the default image
      defaultimgfile.destroy()

      #copys the image and converts it so that it has a numeric value of rgb
      imgfcalc = Image.open(userfile)
      width, height = imgfcalc.size
      image_rgb = imgfcalc.convert("RGB")

      loadontocanvas(0)
      #puts image on canvas
  imgangle1 = 0
  def loadontocanvas(rotationalangle):
      global resized
      global finalpic
      global userchosenimg1
      global userchosenimg

      print("loading...")
      try:
          my_canvas.delete(userchosenimg1)
      except:
          print("")
      numforrez = (width*175)/height

      userchosenimg = Image.open(userfile)
      resized = userchosenimg.resize((int(numforrez), 175), Image.ANTIALIAS)
      resized = resized.rotate(angle=imgangle1)
      finalpic = ImageTk.PhotoImage(resized)


      userchosenimg1 = my_canvas.create_image(195, 105,image=finalpic)


      print("successfully imported image")

      rotateimgbtn.place(x=50, y=300)

      convertbtn.place(x=207, y=28)
  #end of function

  rotq = ""
  rotp = ""
  def rotatetheimg():
      global imgangle1
      global rotq
      global rotp


      imgangle1 += 90
      if imgangle1 == 360:
          imgangle1 = 0

      if imgangle1 == 90:
          rotq = "-"
      elif imgangle1 == 180:
          rotp = "-"
      elif imgangle1 == 270:
          rotq = "-"
          rotp = "-"
      elif imgangle1 == 0:
          rotq = ""
          rotp = ""
      loadontocanvas(imgangle1)


  #the name of the default image file displayed when the window first starts up
  defaultimgfile = Label(root, text='PixelCraftLogo.png')
  defaultimgfile.place(x=40, y=30)


  startupText = Label(root, text='Select an image file to create a blueprint!', font=("calibri", 12))
  startupText.place(x=0, y=0)



  imageLabelText = Label(root, text='Image:', font=("Arial", 8, 'bold'))
  imageLabelText.place(x=0, y=30)


  rotateimgbtn = Button(root, text='⭯', style = 'W.TButton', command=rotatetheimg)

  importimages1 = Button(root, text='Browse', style = 'W.TButton', command=importimages)
  importimages1.place(x=160, y=30)


  convertbtn= Button(root, text='Create my files!', style = 'W.TButton', command=filenameinput)
  ent = Entry(root, width=20)
  confbutton = Button(root, text='Confirm', style = 'W.TButton', command=warningmaker)


  root.mainloop()

好的,所以错误来了,因为你写了

imgfcalc.getpixel(i,j)

并且根据错误, getpixel方法仅采用两个位置参数。 但是你给了 i , j 是两个位置参数,一个位置参数是imgfcalc(self) 因此,您总共给出了 3 个参数并且您需要删除一个参数。 该函数可能会采用元组/列表中的那些 i,j 参数尝试将参数作为 (i,j) 而不是 i,j

关于 tkinter 的笔记,可以访问: https ://mycodenotein.netlify.app

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM