SytanxError with sg. argument from PySimpleGUI in Python - python

I want to make a widget window, but the Bottom and Text part aren't working properly and it keps getting sintax errors at the same part:
import PySimpleGUI as sg
layout = [
[sg.Text("Hello from PySimpleGUI")],
[sg.Button("Close")]
window = sg.Window("Demo, layout")
]
while true:
event, values = window.read()
if event == "Close" or event == sg.WIN_CLOSED:
break
window.close()
it says I forgot a comma, but the references I checked for this code didn't use one, and I tried changing the elements or just putting the comma, but it didn't work either.
ERROR message:
line 5
[sg.Buttom("OK")]
^^^^^^^^^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

This:
layout = [
[sg.Text("Hello from PySimpleGUI")],
[sg.Button("Close")]
window = sg.Window("Demo, layout")
]
isn't a valid list, because you can't include an assignment like that, and there should be a comma after the second element. Also, the call to sg.Window() doesn't look right, because presumably you meant to pass layout as the second argument to define the layout of a window named "Demo".
Try doing this:
layout = [
[sg.Text("Hello from PySimpleGUI")],
[sg.Button("Close")]
]
window = sg.Window("Demo", layout)

If you need to access an object in a list, you either need to create it before hand, like this:
window = sg.Window("Demo")
layout = [
[sg.Text("Hello from PySimpleGUI")],
[sg.Button("Close")],
[window]
]
Or do something ugly that relies on knowing the internal structure, like this:
layout = [
[sg.Text("Hello from PySimpleGUI")],
[sg.Button("Close")],
[sg.Window("Demo")]
]
window = layout[2][0]

Related

table rows not showing properly, only one row is displayed after running

i have written the right code in python simple gui, but after running the code, only one row is displayed and the whole table is not shown i dont know why.this is what is displayed after running the code.
import PySimpleGUI as sg
rows=[
["CULTUS","4","3500","300","550"],
["SONATA","3","5000","350","750"],
["KIA Sportage","3","6000","500","700"],
["Yaris","5","4000","300","600"],
["HONDA CIVIC","5","5500","300","600"],
["Pajero","2","7000","500","700"]
]
header =[
["Model", "Available", "Price/Day","Liability Insurance/Day", "Comprehensive Insurance/Day"]
]
layout=[[sg.Table(
values=rows,
headings=header,
size=(500,300),
key="-TABLE-",)
],
[sg.OK(),sg.Cancel()]
]
window = sg.Window('TG Enterprises', layout ,size=(600,400))
event, values = window.read()
if event == 'Cancel':
window.close()
print(event,values)
this is my code ,can anyone please help me what is wrong with this code and what can i do to make it right?
I cut out some of the text to make it easier to comprehend, but basically it turns out that your attempt was very close.
The main error was the header was a list containing a list. whereas it should just have been a list.
so using boilerplate i made a working example for you like this:
import PySimpleGUI as sg
rows=[
["CULTUS","4","3500","300","550"],
["SONATA","3","5000","350","750"],
["KIA Sportage","3","6000","500","700"],
["Yaris","5","4000","300","600"],
["HONDA CIVIC","5","5500","300","600"],
["Pajero","2","7000","500","700"]
]
header =["Model", "Available", "Price","Liability", "Comprehensive"]
layout = [[sg.Text(x) for x in header],
[sg.Table(values=rows,
headings=header,
max_col_width=25,
auto_size_columns=True,
justification='right',
alternating_row_color='lightblue',
key='table')],
[sg.Button('Add'), sg.Button('Delete')]]
window = sg.Window('Table example').Layout(layout)
while True:
event, values = window.Read()
if event is None:
break
print(event, values)
window.Close()
and the result looks like this:
note: I didnt try out the buttons or any other logic. I just provided an example of the correct formatting example.

PySimpleGUI tables - unable to create list of lists

I'm unable to use PySimpleGUI.Table because of my inability to create list of lists, please see my code:
def is_compiled(file):
full_path = main_path + 'models_data/'+ file[:-3]
return isdir(full_path) #Returns bool True of False
models_src = [f for f in listdir(model_src_path) if isfile(join(model_src_path, f))]
is_compiled_list = [str(is_compiled(f)) for f in models_src]
table_list = [models_src,is_compiled_list]
print(table_list)
printing my lists shows:
[['CNN_v1.py', 'test.py'], ['False', 'True']]
and type is <class 'list'>
But when I try to put this list into sg.Table:
table_headings = ['name','iscompiled?']
layout = [[sg.Table(table_list,headings=table_headings]]
window = sg.Window("Demo", layout, margins=(500, 300))
while True:
event, values = window.read()
I'm getting this error message:
list indices must be integers or slices, not Text
I know this is probably a very simple solution but I was trying to find it for hours and I couldn't. Thanks for help!
EDIT:Typo
This exception maybe caused by the code you not show here.
For example, in following code, [sg.Table(table_list, headings=table_headings)] come with [sg.Text('Hellow World')] at next line.
import PySimpleGUI as sg
table_list = [['CNN_v1.py', 'test.py'], ['False', 'True']]
table_headings = ['name','iscompiled?']
layout = [
[sg.Table(table_list, headings=table_headings)]
[sg.Text('Hellow World')]
]
sg.Window('Title', layout).read(close=True)
It is something like following code, sg.Text('Hellow World') will be thought as the index of list1, that's why you got exception TypeError: list indices must be integers or slices, not Text
list1 = [sg.Table(table_list, headings=table_headings)]
layout = [
list1[sg.Text('Hellow World')]
]
It should be with a comma between any two items in a list, like
import PySimpleGUI as sg
table_list = [['CNN_v1.py', 'test.py'], ['False', 'True']]
table_headings = ['name','iscompiled?']
layout = [
[sg.Table(table_list, headings=table_headings)], # A comma missed between any two items
[sg.Text('Hellow World')]
]
sg.Window('Title', layout).read(close=True)

How do you add pageup/pagedown keybindings to TextArea in python-prompt-toolkit?

Let's use calculator.py for example.
To add a scrollbar that works with the mouse wheel, you would change:
output_field = TextArea(style="class:output-field", text=help_text)
to:
output_field = TextArea(style="class:output-field", text=help_text, scrollbar=True)
But what would you add or change to scroll the TextArea with the page up and page down keys?
# The key bindings.
kb = KeyBindings()
#kb.add("pageup")
def _(event):
# What goes here?
pass
#kb.add("pagedown")
def _(event):
# What goes here?
pass
Change focus
The simplest way would probably be to import focus_next (or focus_previous)
from prompt_toolkit.key_binding.bindings.focus import focus_next
and bind it to Control-Space (or anything else).
# The key bindings.
kb = KeyBindings()
kb.add("c-space")(focus_next)
Keep focus
You could also, to seemingly keep the focus on input_field, import scroll_page_up and scroll_page_down
from prompt_toolkit.key_binding.bindings.page_navigation import scroll_page_up, scroll_page_down
then switch focus to output_field, call scroll_page_up/scroll_page_down and finally change focus back to input_field.
# The key bindings.
kb = KeyBindings()
#kb.add("pageup")
def _(event):
w = event.app.layout.current_window
event.app.layout.focus(output_field.window)
scroll_page_up(event)
event.app.layout.focus(w)
#kb.add("pagedown")
def _(event):
w = event.app.layout.current_window
event.app.layout.focus(output_field.window)
scroll_page_down(event)
event.app.layout.focus(w)

PySimpleGUI get selected extension from FileSaveAs dialog box

I created a FileSaveAs button in my PySimpleGUI application, and defined the available file_types to be 'png' and 'jpg', but I have no way of knowing which of these two options was selected by the user. In other words, unless explicitly entered by the user, the value I get does not include the file extension.
Here's the code:
import PySimpleGUI as sg
layout = [[
sg.InputText(visible=False, enable_events=True, key='fig_path'),
sg.FileSaveAs(
key='fig_save',
file_types=(('PNG', '.png'), ('JPG', '.jpg')), # TODO: better names
)
]]
window = sg.Window('Demo Application', layout, finalize=True)
fig_canvas_agg = None
while True: # Event Loop
event, values = window.Read()
if (event == 'fig_path') and (values['fig_path'] != ''):
print('Saving to:', values['fig_path'])
if event is None:
break
Example:
In the above case, the value will be "[some path]\Test\hello", instead of ending with "hello.png".
Any way of either getting the returned path to include the extension, or getting the extension value separately?
Add defaultextension="*.*" to tk.filedialog.asksaveasfilename ()
It's around line 3332 in ver 4.30.0 of pysimplegui.py

WxPython BoxSizer inside Listbook

I'm trying to use wx.Listbook to implement a settings window with multiple pages. I made the Listbook just fine, but when I started adding items to a page, I ran into a problem. The items are displayed on top of each other, so it's impossible to see everything I'm trying to show.
self.global_settings_frame = wx.Frame(parent=self, title="Global Settings", name="Global Settings")
self.global_settings_listbook = wx.Listbook(parent=self.global_settings_frame, style=wx.LB_LEFT)
self.global_settings_file_window = wx.Panel(parent=self.global_settings_listbook)
self.global_settings_file_box = wx.BoxSizer(orient=wx.VERTICAL)
self.show_full_pathname_checkbox = wx.CheckBox(self.global_settings_file_window, label="Show full pathname")
self.global_settings_file_box.Add(self.show_full_pathname_checkbox, proportion=1)
self.global_default_extension = wx.TextCtrl(self.global_settings_file_window)
self.global_settings_file_box.Add(self.global_default_extension, proportion=1)
self.global_settings_token_window = wx.Panel(parent=self.global_settings_listbook)
self.global_settings_listbook.InsertPage(0, self.global_settings_file_window, "Files")
self.global_settings_listbook.InsertPage(1, self.global_settings_token_window, "Token Defnition")
self.global_settings_frame.Show()
When I comment out the second element, the uncommented part works fine:
self.global_settings_frame = wx.Frame(parent=self, title="Global Settings", name="Global Settings")
self.global_settings_listbook = wx.Listbook(parent=self.global_settings_frame, style=wx.LB_LEFT)
self.global_settings_file_window = wx.Panel(parent=self.global_settings_listbook)
self.global_settings_file_box = wx.BoxSizer(orient=wx.VERTICAL)
self.show_full_pathname_checkbox = wx.CheckBox(self.global_settings_file_window, label="Show full pathname")
self.global_settings_file_box.Add(self.show_full_pathname_checkbox, proportion=1)
self.global_default_extension = wx.TextCtrl(self.global_settings_file_window)
self.global_settings_file_box.Add(self.global_default_extension, proportion=1)
self.global_settings_token_window = wx.Panel(parent=self.global_settings_listbook)
self.global_settings_listbook.InsertPage(0, self.global_settings_file_window, "Files")
self.global_settings_listbook.InsertPage(1, self.global_settings_token_window, "Token Defnition")
self.global_settings_frame.Show()
But I think the BoxSizer isn't working right because when I comment out the previous line (the one adding the CheckBox to the BoxSizer), the display is the same.
I've tried using separate panels for each element and then putting those panels in the BoxSizer, but that also didn't work (I can show you what that looks like if necessary). So it looks like I'm not using the BoxSizer correctly, but I don't understand how I am supposed to use it in this case. What I want is a page of a ListBook that contains a CheckBox and a TextCtrl (for single line text entry). Can you help?
As I can see, you never assign any sizer to your page(s).
You should just do it :
......
self.global_settings_file_box.Add(self.global_default_extension, proportion=1)
self.global_settings_file_window.SetSizer(self.global_settings_file_box)
self.global_settings_token_window = wx.Panel(parent=self.global_settings_listbook)
......
Regards
Xav'

Categories

Resources