Create a folder selection menu in npyscreen - python

I'm working on a npyscreen interface and want a popup with a folderselection, but I don't know how to do it. I have a working basic Interface and within menu are 2 Options ("C1" and "C2"). For example: Select C1 -> hit enter -> Popup appears and let me pick a folder. At the second step, the selection should be displayed at the status box:
"C1: [selected_folder]"
This is the code for the Interface:
class qmkdevice(npyscreen.NPSApp):
def main(self):
F = npyscreen.Form(name = "DEVICE", lines=20, columns=53)
column_height = terminal_dimensions()[0] -9
widget_top = F.add(
Column,
name = "MENU",
relx = 2,
rely = 7,
max_width = 48,
max_height = 11,
)
widget_bottom = F.add(
Column,
name = "STATUS",
relx = 2,
rely = 2,
max_width = 48,
max_height = 5,
)
widget_top.values = ["C1", "C2"]
widget_bottom.values = ["C1: [ ]", "C2: [ ]"]
F.edit()
print(ms.get_selected_objects())
class Column(npyscreen.BoxTitle):
def resize(self):
self.max_height = int(0.73 * terminal_dimensions()[0])
def terminal_dimensions():
return curses.initscr().getmaxyx()
Thanks for your help.

Related

How does Flet make the Datatable always at the top?

I'm trying to implement a desktop application. However, I'm encountering some issues.
When I try to insert my data using the DataTable implementation, the layout of my data table may not appear at the top or the layout and length of the data table may affect the layout of other components after inserting the data.
If you know how to solve this problem, I would appreciate your advice or provide me with reference documentation. Thank you very much.
Here's the example code I wrote
import flet as ft
class MainWindow(UserControl):
def build(self):
self.Entry_URL = TextField(label = 'input keyword', width = 300)
searchLine = Row(spacing = 70, controls = [
ElevatedButton(text = 'btn1', on_click = self.startSpider),
ElevatedButton(text = 'btn2', disabled = True)])
ControlLine = Row(spacing = 70, controls = [
ElevatedButton(text = 'btn3'),
ElevatedButton(text = 'btn4', disabled = True)])
self.Table = DataTable(
columns = [DataColumn(Checkbox(label = 'Info')),
DataColumn(Text('col1')),
DataColumn(Text('col2')),
DataColumn(Text('col3')),
],
)
return Column(controls = [Row(controls = [self.Entry_URL, self.Table]), searchLine, ControlLine,
ElevatedButton(text = 'quit', width = 300), ], scroll = ft.ScrollMode.AUTO)
def startSpider(self, event):
# if (not self.Entry_URL.value):
# return
self.Table.rows.append(ft.DataRow(
cells = [
ft.DataCell(ft.Text("John", text_align = ft.TextAlign.CENTER)),
ft.DataCell(ft.Text("Smith", text_align = ft.TextAlign.CENTER)),
ft.DataCell(ft.Text("43", text_align = ft.TextAlign.CENTER)),
ft.DataCell(ft.Text("43", text_align = ft.TextAlign.CENTER), ),
],
),)
self.update()
def main(page:Page):
page.title = 'Test'
page.padding = 20
page.add(MainWindows())
ft.app(target = main)

How do you query an object inside of a text field, to do something with it?

I would like to know how to query a selection entered into a text field group, so I can do something with it. I have created a window to just translate an object that I loaded in the text field. The error is that cont is not defined.
import maya.cmds as cmds
import maya.mel as ml
def set_selected_name (text_field):
cont = cmds.ls (selection = True)
text_field = cmds.textFieldButtonGrp (text_field, edit = True,
text = ''.join (cont),
buttonLabel = '<<<<',
backgroundColor = [0.5098039215686274,
0.5098039215686274,
0.5098039215686274])
return text_field
def translate_x(cont):
cmds.setAttr( cont[0] + '.translateX', 10)
def translate_y():
cmds.setAttr( cont[0] + '.translateY', 10)
def translate_z(*Args):
cmds.setAttr( cont[0] + '.translateZ', 10)
if cmds.window ('window1', q = 1, ex = 1):
cmds.deleteUI ('window1')
cmds.window ('window1',
title = 'Translate Attr',
sizeable = 0,
resizeToFitChildren = True,
menuBar = 1)
cmds.rowLayout (numberOfColumns = 3)
cmds.separator (style = 'double',
height = 6)
cmds.rowLayout (parent = 'window1',
numberOfColumns = 4)
ddd = cmds.textFieldButtonGrp (editable = False,
text = 'Obj',
backgroundColor = [0.029495689326314183,
0.5488975356679637,
0.5488975356679637],
buttonLabel = '<<<<')
cmds.textFieldButtonGrp (ddd, edit = True,
buttonCommand = 'set_selected_name (ddd)')
cmds.separator (style = 'double',
height = 6)
cmds.rowLayout (parent = 'window1',
numberOfColumns = 6)
cmds.separator (style = 'double',
height = 6)
cmds.button (command = 'translate_y()',
backgroundColor = [1.0,
0.7300068665598535,
0.7300068665598535],
label = 'Translate Y')
cmds.separator (style = 'double',
height = 6)
cmds.button (command = 'translate_x(cont)',
backgroundColor = [1.0,
0.9733272297245746,
0.7333333333333333],
label = 'Translate X')
cmds.separator (style = 'double',
height = 6)
cmds.button (command = 'translate_z()',
backgroundColor = [0.7333333333333333,
1.0,
0.7600061036087586],
label = 'Translate Z')
cmds.columnLayout (parent = 'window1')
cmds.separator (style = 'double',
height = 3)
cmds.showWindow ('window1')
# ~ BABLAAM ~
Create any object you like, loaded into the text field and then try to translate with buttons.
You have several problems in your code.
In the translate commands you always use cont[0]. cont is only used in the function set_selected_name() and is a local variable what means it is deleted as soon as the function is completed.
You can use a string as command in the button command, but this only works with static values. You should use lambdas to use functions with arguments.
The cont Problem can be solved by using a global variable, but it shouldn't since global variables are the source of all evil. A much more elegant way would be to enclose you UI in one python class and use instance variables to get the selection.
I have adjusted the code to add the class as you recommended but still having the same issue. By not using the quotes in the button command I get this error when I try to run the script, instead of getting it when I press the button.
Error: NameError: file line 28: name 'translate_x' is not defined
Can you please write a workable version, or place a link from the internet that shows a method using the class and calling methods using buttons? Nothing I have found from my internet search has anything like this and I'm just guessing where thigs should go.
import maya.cmds as cmds
import maya.mel as ml
class move_obj(object):
def __int__(self, *args):
self.cont = cont
self.trans = trans
def set_selected_name(self, *args):
cont = cmds.ls (selection = True)
return cont
def translate_x(self, *args):
trans = cmds.setAttr( cont[0] + '.translateX', 10)
print trans
if cmds.window ('window1', q = 1, ex = 1):
cmds.deleteUI ('window1')
cmds.window ('window1',
title = 'Translate Attr',
sizeable = 0,
resizeToFitChildren = True,
menuBar = 1)
cmds.rowLayout (numberOfColumns = 3)
cmds.button (command = translate_x,
backgroundColor = [1.0,
0.7300068665598535,
0.7300068665598535],
label = 'Translate X')
cmds.showWindow ('window1')

Why wont my code return a value for the data input into a tkinter text box

I have written this code and for some reason it refuses to return any sort of value or input for slef.REV when used in the function post(self) however it will return a value when I try and return a value in the getlen() function which is used to reurn the number of characters in the review.I dont have this problem for any other variables that I retrieve data from within this class. Below is the relevant code, any help would be appreciated. the lines where this problem occures is the first functio calld post(lines 1-5) and 4 lines up from the bottom
def post(self):
MovieID = self.MovID
REV = self.REV
AddReview(conn,cursor,Add_Review,MovieID,REV)
print(REV)
def shrek_film(self):
self.title = "Shrek"
self.MovID = 1
self.root4 = tk.Toplevel()
self.root4.title("Watch Shreck")
self.root4.geometry("1400x800")
frame_4 = tk.Frame(self.root4, bg = "black")
frame_4.pack(fill = tk.BOTH, expand = True, padx=0 , pady=0)
frame_4.grid_columnconfigure(1,weight=1)
self.Create_canvas = tk.Canvas(frame_4, width=2000, height=1080)
self.Create_canvas.place(x=-50, y=-50)
self.Create_img = PhotoImage(file="shrek-landscape.gif")
self.Create_canvas.create_image(20, 20, anchor = NW, image=self.Create_img)
play_button= tk.Button(frame_4,bg="orange",text="play", command = self.addHistory)
play_button.place(x=700,y=400)
play_button.config(font=("Ariel","30"))
def gtelen():
Review = reviewbox.get('1.0',END)
REVLEN = len(Review)
REVLENLEFT = (231-len(Review))
if REVLEN >=230:
lenbox = tk.Label(frame_4 ,text="No words left",bg="orange")
lenbox.place(x=360,y=460)
lenbox.config(font=("Ariel","15"))
else:
lenbox = tk.Label(frame_4 ,text=REVLENLEFT,bg="orange")
lenbox.place(x=360,y=460)
lenbox.config(font=("Ariel","15"))
print(Review)
Words_button = tk.Button(frame_4, bg="orange",text="check number of words remaining", command=gtelen)
Words_button.place(x=150,y=460)
Words_button.config(font=("Ariel","10"))
reviewlable=tk.Label(frame_4,text="Write a review",bg="orange")
reviewlable.place(x=10,y=460)
reviewlable.config(font=("ariel","15"))
Review_button= tk.Button(frame_4,bg="orange",text="See Reviews")#, command = self.ViewReviews)
Review_button.place(x=490,y=450)
Review_button.config(font=("Ariel","15"))
reviewbox= Text(frame_4,width=100,height=12)
reviewbox.place(x=10,y=500)
self.REV = reviewbox.get('1.0',END)
post_button = tk.Button(frame_4,bg="orange",text="Post Review", command = self.post)
post_button.place(x=830,y=650)
post_button.config(font=("Ariel","15"))
You can use Entry instead and use a StringVar
v = StringVar() # Create StringVar
reviewbox = Entry(frame_4, width = 100, height = 12, textvariable = v) # Create Entry widget
reviewbox.place(x = 10, y = 500) # Place Entry widget
self.REV = v.get() # Get contents of StringVar
The line self.REV = reviewbox.get('1.0',END) is being called about a millisecond after creating the text widget. The user will not even have seen the widget yet, much less have had time to type in it.
You can't call the get() method until after the user has had a chance to enter data, such as inside the post method.
def post(self):
MovieID = self.MovID
REV = reviewbox.get("1.0", "end")
AddReview(conn,cursor,Add_Review,MovieID,REV)
print(REV)

create variable name from list using a loop in python

I am trying to generate a bunch of variables from a list, which will be called from another function.
If I create them separately(as in the commented part of the code), everything is working fine(by fine, I mean, I can access the date using get_data function listed below). But, when I am using the loop, the get_data is giving error:
File "main.py", line 97, in get_data
dAu = self.Author.get_text()
AttributeError: 'MyWindow' object has no attribute 'Author'
So, it is obvious that inside the loop, EDITI am expecting, for field[0],
self.field = self.field[0]=self.Author
but this is not the case.EDIT COMPLETE
self.field != self.Author
as I wished.
How can I get that?
The code in question is:
# Generate the Entry fields
self.notebook = Gtk.Notebook()
# self.LAuthor = Gtk.Label("Author")
# self.EAuthor = Gtk.Entry()
# self.page = Gtk.Grid()
# self.page.attach(self.LAuthor, 0, 0, 2, 1)
# self.page.attach_next_to(self.EAuthor, self.LAuthor, Gtk.PositionType.RIGHT, 1, 1)
# self.notebook.append_page(self.page, Gtk.Label("Trial"))
xpos = 0
minf = 0
fields = ["Author", "Year", "Journal", "Title", "Publisher", "Page",
"Address", "Annote", " Booktitle", "Chapter", "Crossred",
"Edition", "Editor", "HowPublished", "Institution", "Month",
"Note", "Number", "Organization", "Pages", "Publishers",
"School", "Series", "Type"]
Tabs = ["Essential", "Publishers", "Extra I", "Extra II"]
for note in range(int(len(fields)/6)):
ypos = 0
self.npage = "page"+str(note)
self.npage = Gtk.Grid()
self.npage.set_border_width(10)
maxf = minf+6
for field in fields[minf:maxf]:
print(field)
self.lfield = "L" + field
self.lfield = Gtk.Label(field)
self.field = Gtk.Entry()
self.field.set_placeholder_text(field)
self.npage.attach(self.lfield, xpos, ypos, 2, 1)
self.npage.attach_next_to(self.field, self.lfield,
Gtk.PositionType.RIGHT, 1, 1)
ypos += 1
self.notebook.append_page(self.npage, Gtk.Label(Tabs[note]))
minf = maxf
And the get_data function is just:
def get_data(self, widget):
dAu = self.Author.get_text()
print(dAu)
Kindly help.
Use dictionary - for example:
self.all_fields = dict()
field = "Author"
self.all_fields[field] = ...
# self.all_fields["Author"] = ...
and then you can use
def get_data(self, widget):
dAu = self.all_fields["Author"].get_text()
print(dAu)

Python - Change the textField after browsing - MAYA

I've got the most annoying problem with my GUI Exporter in Maya. I've made the textField etc. work, but I can't change the value of the textField after it's created, which is what I need to do.
What I want to do for example is, let's say the filepath is none from the beginning. The textField has now printed out: "None" in it, but after you press browse and select a directory, I want it to change None to the directory path etc.
That's the only problem I currently have and the error code received is this:
Error: RuntimeError: file C:\Program Files\Autodesk\Maya2015\Python\lib\site-packages\pymel\internal\pmcmds.py line 134: Too many children in layout: rowLayout3 #
Code:
#Setup the window using the returned window
def setupWindow(self, new_window):
try:
frame_layout = pm.frameLayout(labelVisible = False, marginWidth = 5, marginHeight = 5)
pm.columnLayout(w = 350, h = 300)
pm.text(label = "Filepath: ")
self.textField = pm.textField("FieldNorm", text = "%s" % self.filePath, editable = False, w = 350, h = 20)
pm.button(label = "Browse", w = 100, h = 20, command = self.browse)
pm.rowLayout(numberOfColumns = 2, adjustableColumn = 1, w = 350, h = 25)
pm.button(label = "Export", w = 200, h = 25, command = self.export)
pm.button(label = "Close", w = 100, h = 25, command = pm.Callback(self.closeButton, new_window))
except:
print "<Setting up window failed>"
#Show the returned window
def showWindow(self, new_window):
if new_window is not None:
pm.showWindow(new_window)
else:
print "<Window does not exist!>"
#Browse Directory and Paste into textField
def browse(self, filePath):
self.filePath = pm.fileDialog2(dialogStyle = 2, returnFilter = 1, fileFilter = "*.obj")
if self.filePath is not None:
self.textField = pm.textField("FieldNorm", text = "%s" % self.filePath, editable = False, w = 350, h = 20)
else:
print "<No changes has been made!>"
Looks like you need the edit flag in the pm.textField line in browse()
pm.textField("FieldNorm", edit=True, text = "%s" % self.filePath)
The error means you are adding a new control, probably to the rowlayout at the end of the setupWindow function which holds the two buttons - Maya thinks you're adding a third
If you want to update the contents of self.textfield in the browse functions, you want
pm.textField(self.textField, e=True, text = "%s" % self.filePath, editable = False, w = 350, h = 20)
which will edit the already created field. The line in the example
self.textField = pm.textField("FieldNorm", text = "%s" % self.filePath, editable = False, w = 350, h = 20)
is trying to create a new one, as #julianMann points out

Categories