Home > Net >  python tkinter giving false errors i think
python tkinter giving false errors i think

Time:08-14

my code:

import tkinter as Tk

def B():
    print(":D")
button2 = Button(Tk, text="hello", command=B)
button2.place(x=50, y=0)
button2.pack()
Tk.wm_title("button test")
Tk.geometry("320x200")
Tk.mainloop()

but i get this as an output:

Traceback (most recent call last):
  File "C:/Users/user/Desktop/test_1.py", line 7, in <module>
    button2 = Button(Tk, text="hello", command=B)
TypeError: Button.__init__() got multiple values for argument 'text'

can someone help me out with this one because i cant find a way to fix this error

CodePudding user response:

change

button2 = Button(Tk, text="hello", command=B)

to

button2 = Tk.Button(your_root_window, text="hello", command=B)

you also haven't create a root window, and you can't call it 'Tk'. so, you're code is almost completely wrong. Computer is never wrong ;)

CodePudding user response:

Here is the full code

from tkinter import *

def B():
    print(":D")

root = Tk()

button2 = Button(root, text="hello", command=B)
button2.place(x=50, y=0)
root.title("button test")
root.geometry("320x200")
root.mainloop()

Importing tkinter the way I did is faster and will cause less errors as you will need to Tk. more less

If you imported tkinter this way, adding the root=Tk() will be very important.

If you are going to use .place to position any widget in tkinter don't use .pack as it will prevent the widget from being placed at the position you entered.

CodePudding user response:

First, you need to make window like this root=Tk.Tk()(root is the name of the window) before Create controls.

Second, as you import tkinter like import tkinter as Tk. You should use Tk. before each sentense before using tkinter. Such as button2 = Button(Tk, text="hello", command=B), should change to button2 = Tk.Button(root, text="hello", command=B) (root is the window name create before)

Here is my code:

import tkinter as Tk

def B():
    print(":D")


root = Tk.Tk()
root.wm_title("button test")
root.geometry("320x200")
button2 = Tk.Button(root, text="hello", command=B)
button2.place(x=50, y=0)
button2.pack()
Tk.mainloop()
  • Related