首页 > 解决方案 > PySimpleGUI 中的 window.Read() 函数出现错误

问题描述

我是编程新手,刚开始使用 PySimpleGUI 尝试二十一点游戏。我不知道为什么我在 line event,values = window.Read() 上得到错误。任何帮助表示赞赏。

import PySimpleGUI as sg
import random

# Creating multiple varibales to be used throughout
deck = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
hand = []
dealer_hand = []
random1 = random.randint(1,10)
random2 = random.randint(1,10)
dealer1 = random.randint(1,10)
dealer2 = random.randint(1,10)
welcome =  [ [sg.Text('Welcome to blackjack', text_color='#000000', font=('Times New Roman', 
16,'bold'))],
[sg.Text('Rules: You will be dealt 2 cards and the same for the dealer. The dealer will', 
text_color='#000000')],
[sg.Text(' only reveal one of his cards. Your objective is to reach close to 21 without crossing it',
                  text_color='#000000')],
         [sg.Text(' Aces can be 1 or 11, all face cards are 10. If you choose to stand, your total 
should be more than the dealer'
                  ,
                    text_color='#000000')],
         [sg.Button('Start!')]]
window = sg.Window('Welcome', background_color = '#FFFFFF').Layout(welcome)
window.read()
window.close()

first = [ [sg.Text('The dealer has dealt the cards:', font=('Times New Roman'))],
          [sg.Text(f'Your two starting cards are {random1} and {random2}')],
          [sg.Text(f'Dealer shows his faced up card: {dealer1}')],
          [sg.Button('Hit'), sg.Button('Stand')] ]
       

window = sg.Window('First set', first)

event, values = window.Read()
print(event)

这是我得到的很长的错误:

Traceback (most recent call last):
  File "C:\Users\j\Desktop\Applied Computing\Python\blackjack.py", line 32, in <module>
    event, values = window.Read()
  File "C:\Users\j\AppData\Roaming\Python\Python39\site-packages\PySimpleGUI\PySimpleGUI.py", line 8328, in read
    results = self._read(timeout=timeout, timeout_key=timeout_key)
  File "C:\Users\j\AppData\Roaming\Python\Python39\site-packages\PySimpleGUI\PySimpleGUI.py", line 8394, in _read
    self._Show()
  File "C:\Users\j\AppData\Roaming\Python\Python39\site-packages\PySimpleGUI\PySimpleGUI.py", line 8155, in _Show
    StartupTK(self)
  File "C:\Users\j\AppData\Roaming\Python\Python39\site-packages\PySimpleGUI\PySimpleGUI.py", line 13996, in StartupTK
    _convert_window_to_tk(window)
  File "C:\Users\j\AppData\Roaming\Python\Python39\site-packages\PySimpleGUI\PySimpleGUI.py", line 13868, in _convert_window_to_tk
    PackFormIntoFrame(window, master, window)
  File "C:\Users\j\AppData\Roaming\Python\Python39\site-packages\PySimpleGUI\PySimpleGUI.py", line 12403, in PackFormIntoFrame
    tktext_label = element.Widget = tk.Label(tk_row_frame, textvariable=stringvar, width=width,
  File "C:\Users\j\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 3148, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Users\j\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2572, in __init__
    self.tk.call(
_tkinter.TclError: expected integer but got "New"

标签: pythoneventswindowdisplaypysimplegui

解决方案


你在这里设置了错误的字体

first = [ [sg.Text('The dealer has dealt the cards:', font=('Times New Roman'))],
          [sg.Text(f'Your two starting cards are {random1} and {random2}')],
          [sg.Text(f'Dealer shows his faced up card: {dealer1}')],
          [sg.Button('Hit'), sg.Button('Stand')] ]

字体作为一个元组,其第一个元素是字体系列,后跟一个大小(如果为正则以磅为单位,如果为负则以像素为单位),可选地后跟一个包含一个或多个样式修饰符的字符串粗体、斜体、下划线和覆盖。

你错过了sizefor 选项font


推荐阅读