Execution error on library startup

Asked

Viewed 41 times

0

import sys
import tkinter

counter = 0

def update():
    global count, b
    counter += 1
    b.config(text = "click cont = " + str(count))
    print("updating")

root = tkinter.Tk()
b = tkinter.Button(root)
b.configure(background="yellow", text="click count = 0", command=update)
b.pack()
root.mainloop()
Traceback (most recent call last):
  File "C:/Users/miopo/untitled-1.py", line 12, in <module>
    root = tkinter.Tk()
  File "C:\Users\miopo\AppData\Local\Programs\Python\Python36-32\Lib\tkinter\__init__.py", line 2012, in __init__
    baseName = os.path.basename(sys.argv[0])
builtins.IndexError: list index out of range
  • i use Wing 101 as ide

  • How did you execute the file? By default, if the class Tk do not possess the value of basename defined in your call it will seek the name of the script executed from the command line arguments; and the first argument, sys.argv[0], always is defined in the execution.

  • i ran using Wing 101 I just typed the code in the ide and selected run debug

  • Enzo, tries to run your file direct from the command line, other than by IDE.

  • From the command line your code worked perfectly here: https://i.imgur.com/aEAgvyS.jpg

1 answer

-2

I posted as an answer because I still can not comment on other people’s questions

Ever tried to use
from tkinter import *
root = Tk() ?

I made some adaptations to the code that made it run smoothly here for me:

The update method (here on my machine) could not access the variable counter, so I put her in a class along with the responsibility of increment.

import sys
import tkinter

class Contador():
    def __init__(self):
        self.valor = 0

    def incrementa(self):
        self.valor += 1

contador = Contador()

def update():
    global count, b
    contador.incrementa()
    count = str(contador.valor)
    b.config(text = "click cont = " + count)
    print("updating")

root = tkinter.Tk()
b = tkinter.Button(root)
b.configure(background="yellow", text="click count = 0", command=update)
b.pack()
root.mainloop()
  • 1

    And why would that solve the problem?

  • yes more even so give error it also happens with other modules as Turtle and besides that I can open a window of Tkinter in interpratator

  • 1
  • Can’t the error just be logical? And not in import? builtins.IndexError: list index out of range

  • I used the module Math that way and it worked correctly

Browser other questions tagged

You are not signed in. Login or sign up in order to post.