0

Alright so im messing around with a tutorial i followed, and now im trying to get an icon on it (not part of the tut) i've gone through what documentation on it i could find, and followed some different tutorials on how to, whenever i run the code i get

C:\Users\kamron\Desktop>python filenamehere.py
Traceback (most recent call last):
  File "filenamehere.py", line 9, in <module>
    window = sg.Window('Bot', layout, icon)
NameError: name 'icon' is not defined

C:\Users\******\Desktop>python sayoribot.py
  File "filenamehere.py", line 8
    icon=(icon='C:\\Users\\******\\Desktop\\bot_icon.ico')
              ^
SyntaxError: invalid syntax

and here's the code im using to try and get an icon for my program (which is a whole other issue i have, but hopefully if i figure out my error here i can get it)

import PySimpleGUI as sg
sg.theme('BlueMono')
set_icon =(icon='C:\\Users\\******\\Desktop\\bot_icon.ico')
layout =[[sg.Text('How can i help?'), sg.InputText()],[sg.Button(' Ok '), sg.Button('Quit')]]
window = sg.Window('Bot', layout, icon)

i've messed around with placement of the =, but i dont understand the issue. i did try

set_icon(icon='C:\\Users\\******\\Desktop\\bot_icon.ico')

it came up with another syntax error. any help is greatly appreciated. Thanks!

(No its not exactly a bot im working on its just name placeholders lol.)

copperkam
  • 29
  • 5

1 Answers1

0

The code

set_icon =(icon='C:\\Users\\******\\Desktop\\bot_icon.ico')

Is trying to assign a value to set_icon. However, inside that assignment you have another assignment — to icon. This is not allowed in Python1.

Your attempted fix is better:

set_icon(icon='C:\\Users\\******\\Desktop\\bot_icon.ico')

This is the correct syntax for calling a function named set_icon passing a single named argument. However, you probably haven’t defined set_icon. If you want to use the PySimpleGUI function of that name, you need to specify an object to call it on (i.e. window). But you don’t need to do so since you are passing the icon in its constructor. All you have to do is actually define that icon by assigning a variable.

In other words: remove the set_icon call — just leave the variable assignment:

icon='C:\\Users\\******\\Desktop\\bot_icon.ico'

1 Specifically, it requires using the “walrus operator”, :=

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214