PySimpleGUI:无法完成关键字为0的元素上的操作。

huangapple go评论97阅读模式
英文:

PySimpleGUi: Unable to complete operation on element with key 0

问题

I'm creating a program that asks the user to guess a certain number that the computer will draw. In the tutorial I'm following to make this program, the tutor uses PySimpleGUI to create an interactive window. This is the program code:

  1. import random
  2. import PySimpleGUI as sg
  3. class GuessNumberSimulator():
  4. def __init__(self):
  5. self.random_value = 0
  6. self.min_value = 1
  7. self.max_value = 60
  8. self.number_chances = 10
  9. def Start(self):
  10. layout = [
  11. [sg.Text('Your guess', size=(20,0))],
  12. [sg.Input(size=(18,0), key='GuessValue')],
  13. [sg.Button('Guess!')],
  14. [sg.Output(size=(20,10))]
  15. ]
  16. # create a window
  17. self.window = sg.Window('Guess Number Simulator', layout=layout)
  18. self.GenerateNumber()
  19. self.RequestNumber()
  20. try:
  21. while True:
  22. # receive the values
  23. self.event, self.values = self.window.read()
  24. self.guess_value = self.values['GuessValue']
  25. if self.event == 'Guess!':
  26. while self.number_chances != 0:
  27. if int(self.guess_value) > self.random_value:
  28. print(f'\033[1;32mA little less! (Number of chances: {self.number_chances})\033[m')
  29. self.number_chances -= 1
  30. self.RequestNumber()
  31. elif int(self.guess_value) < self.random_value:
  32. print(f'\033[1;31mA little more! (Number of chances: {self.number_chances})\033[m')
  33. self.number_chances -= 1
  34. self.RequestNumber()
  35. if int(self.guess_value) == self.random_value:
  36. self.try_again = False
  37. print(f'\033[1;34m🎉🎉🎉 CONGRATULATIONS! The number chosen is {self.random_value}! You´re right! 🎉🎉🎉\033[m')
  38. break
  39. if self.number_chances == 0:
  40. print(f'\033[1;35m😢😢😢 What a shame! Your chances are over! The number chosen was {self.random_value}! Better luck next time!\033[m')
  41. except:
  42. print('\033[1;031mPlease insert only integer numbers!\033[m')
  43. def RequestNumber(self):
  44. self.guess_value = input('Guess any number: ')
  45. def GenerateNumber(self):
  46. self.random_value = random.randint(self.min_value, self.max_value)
  47. Guess1 = GuessNumberSimulator()
  48. Guess1.Start()

When I try to run the program, a PySimpleGUI window pops up with this message:

Unable to complete operation on element with key 0. You cannot perform (such as calling update) on an Element until window.read() is called or finalize=True when Window created. Adding a "finalize=True" parameter to your Window creation will likely fix this.

This is a print of the error message:

I tried to check which key the problem was talking about. I tried changing the value of key='GuessValue' to 1, thinking this was the key cited by the error message, but the same message came up. I expected to see the PySimpleGUI interactive window working, but to no avail. The program is running fine in the terminal, but the interactive window doesn't even appear when I try to use the PySimpleGUI in the source code. I tried putting finalize=True" in both self.window = sg.Window('Guess Number Simulator', layout=layout, finalize=True) and self.event, self.values = self.window.read(finalize=True), but the PySimpleGUI window doesn't show anything anymore, it goes blank and doesn't work.

英文:

I'm creating a program that asks the user to guess a certain number that the computer will draw. In the tutorial I'm following to make this program, the tutor uses PySimplesGUI to create an interactive window. This is the program code:

  1. import random
  2. import PySimpleGUI as sg
  3. class GessNumberSimulator():
  4. def __init__(self):
  5. self.randon_value = 0
  6. self.min_value = 1
  7. self.max_value = 60
  8. self.number_chances = 10
  9. def Start(self):
  10. layout = [
  11. [sg.Text(&#39;Your guess&#39;,size=(20,0))],
  12. [sg.Input(size=(18,0),key=&#39;GuessValue&#39;)],
  13. [sg.Button(&#39;Guess!&#39;)],
  14. [sg.Output(size=(20,10))]
  15. ]
  16. # create a window
  17. self.window = sg.Window(&#39;Guess Number Simulator&#39;, layout=layout)
  18. self.GenerateNumber()
  19. self.RequestNumber()
  20. try:
  21. while True:
  22. # receive the values
  23. self.event, self.values = self.window.Read()
  24. self.guess_value = self.values[&#39;GuessValue&#39;]
  25. if self.event == &#39;Guess!&#39;:
  26. while self.number_chances != 0:
  27. if int(self.guess_value) &gt; self.randon_value:
  28. print(f&#39;\033[1;32mA little less! (Number of chances: {self.number_chances})\033[m&#39;)
  29. self.number_chances -= 1
  30. self.RequestNumber()
  31. elif int(self.guess_value) &lt; self.randon_value:
  32. print(f&#39;\033[1;31mA little more! (Number of chances: {self.number_chances})\033[m&#39;)
  33. self.number_chances -= 1
  34. self.RequestNumber()
  35. if int(self.guess_value) == self.randon_value:
  36. self.try_again = False
  37. print(f&#39;\033[1;34m&#127881;&#127881;&#127881; CONGRATULATIONS! The number chosen is {self.randon_value}! You &#180; re right! &#127881;&#127881;&#127881;\033[m&#39;)
  38. break
  39. if self.number_chances == 0:
  40. print(f&#39;\033[1;35m&#128546;&#128546;&#128546; What a shame! Your chances is over! This number chosen was {self.randon_value}! More luck in the next time!\033[m&#39;)
  41. except:
  42. print(&#39;\033[1;031mPlease insert only integers numbers!\033[m&#39;)
  43. def RequestNumber(self):
  44. self.guess_value = input(&#39;Guess any number: &#39;)
  45. def GenerateNumber(self):
  46. self.randon_value = random.randint(self.min_value, self.max_value)
  47. Guess1 = GessNumberSimulator()
  48. Guess1.Start()

When I try to run the program, a PySimpleGUI window pops up with this message:

> Unable to complete operation on element with key 0. You cannot perform (such as calling update) on an Element until window.read() is called or finalize=True when Window created. Adding a "finalize=True" parameter to your Window creation will likely fix this.

This is a print of the error message:

I tried to check which key the problem was talking about. I tried changing the value of key=&#39;GuessValue&#39; to 1 thinking this was the key cited by the error message, but the same message came up. I expected to see the PySimplesGUI interactive window working, but to no avail. The program is running fine in the terminal, but the interactive window doesn't even appear when I try to use the PySimplesGUI in the source code. I tried putting finalize=True&quot; in both self.window = sg.Window(&#39;Guess Number Simulator&#39;, layout=layout, finalize=True) and self.event , self.values ​​= self.window.Read(finalize=True), but the PySimpleGUI window doesn't show anything anymore, it goes blank and doesn't work.

答案1

得分: 1

Here is the translated code:

  1. 找不到代码中的异常要使用 GUI不应同时使用控制台输入或输出
  2. 修订后的代码
  3. ```python
  4. import random
  5. import PySimpleGUI as sg
  6. class GuessNumberSimulator():
  7. def __init__(self):
  8. self.random_value = 0
  9. self.min_value = 1
  10. self.max_value = 60
  11. self.number_chances = 10
  12. def Start(self):
  13. layout = [
  14. [sg.Text('你的猜测'), sg.Input(size=5, key='GuessValue'), sg.Button('猜!', bind_return_key=True)],
  15. [sg.Output(size=(80, 10))]
  16. ]
  17. self.window = sg.Window('猜数字模拟器', layout=layout)
  18. self.GenerateNumber()
  19. while True:
  20. event, values = self.window.read()
  21. if event == sg.WIN_CLOSED:
  22. break
  23. elif event == "猜!":
  24. self.window['GuessValue'].update(select=True)
  25. entry = values['GuessValue']
  26. if not entry.isdigit():
  27. print(f"错误的数字,范围在 {self.min_value}{self.max_value} 之间!")
  28. continue
  29. try:
  30. guess_value = int(entry)
  31. except ValueError:
  32. print(f"错误的数字,范围在 {self.min_value}{self.max_value} 之间!")
  33. continue
  34. if self.number_chances != 0:
  35. if guess_value > self.random_value:
  36. print(f'再小一点!(剩余机会次数:{self.number_chances})')
  37. self.number_chances -= 1
  38. elif guess_value < self.random_value:
  39. print(f'再大一点!(剩余机会次数:{self.number_chances})')
  40. self.number_chances -= 1
  41. elif guess_value == self.random_value:
  42. sg.popup(f'🎉🎉🎉 恭喜你!选择的数字是 {self.random_value}!你猜对了! 🎉🎉🎉')
  43. break
  44. if self.number_chances == 0:
  45. sg.popup(f'😢😢😢 太可惜了!你的机会用完了!选择的数字是 {self.random_value}!下次好运!😢😢😢')
  46. break
  47. self.window.close()
  48. def GenerateNumber(self):
  49. self.random_value = random.randint(self.min_value, self.max_value)
  50. Guess1 = GuessNumberSimulator()
  51. Guess1.Start()

I've translated the code for you.

英文:

Didn't find the exception for your code. To work with a GUI, you should not use console input or output at the same time.

Revised code

  1. import random
  2. import PySimpleGUI as sg
  3. class GessNumberSimulator():
  4. def __init__(self):
  5. self.randon_value = 0
  6. self.min_value = 1
  7. self.max_value = 60
  8. self.number_chances = 10
  9. def Start(self):
  10. layout = [
  11. [sg.Text(&#39;Your guess&#39;), sg.Input(size=5, key=&#39;GuessValue&#39;), sg.Button(&#39;Guess!&#39;, bind_return_key=True)],
  12. [sg.Output(size=(80, 10))]
  13. ]
  14. self.window = sg.Window(&#39;Guess Number Simulator&#39;, layout=layout)
  15. self.GenerateNumber()
  16. while True:
  17. event, values = self.window.read()
  18. if event == sg.WIN_CLOSED:
  19. break
  20. elif event == &quot;Guess!&quot;:
  21. self.window[&#39;GuessValue&#39;].update(select=True)
  22. entry = values[&#39;GuessValue&#39;]
  23. if not entry.isdigit():
  24. print(f&quot;Worng Number, range from {self.min_value} to {self.max_value} !&quot;)
  25. continue
  26. try:
  27. guess_value = int(entry)
  28. except ValueError:
  29. print(f&quot;Worng Number, range from {self.min_value} to {self.max_value} !&quot;)
  30. continue
  31. if self.number_chances != 0:
  32. if guess_value &gt; self.randon_value:
  33. print(f&#39;A little less! (Number of chances: {self.number_chances})&#39;)
  34. self.number_chances -= 1
  35. elif guess_value &lt; self.randon_value:
  36. print(f&#39;A little more! (Number of chances: {self.number_chances})&#39;)
  37. self.number_chances -= 1
  38. elif guess_value == self.randon_value:
  39. sg.popup(f&#39;&#127881;&#127881;&#127881; CONGRATULATIONS! The number chosen is {self.randon_value}! You &#180; re right! &#127881;&#127881;&#127881;&#39;)
  40. break
  41. if self.number_chances == 0:
  42. sg.popup(f&#39;&#128546;&#128546;&#128546; What a shame! Your chances is over! This number chosen was {self.randon_value}! More luck in the next time!&#39;)
  43. break
  44. self.window.close()
  45. def GenerateNumber(self):
  46. self.randon_value = random.randint(self.min_value, self.max_value)
  47. Guess1 = GessNumberSimulator()
  48. Guess1.Start()

huangapple
  • 本文由 发表于 2023年5月17日 20:32:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定