wxPython RichTextCtrl.SetStyle([...]) Issue - python

I want to change the font of a RichTextControl in wxPython but I've encountered an issue that I can not figure out. Here is the relevant code:
import wx
import wx.richtext as rt
#....
codebox = rt.RichTextCtrl(self)
f = wx.Font(10, wx.TELETYPE, wx.NORMAL, wx.NORMAL)
s = wx.TextAttr(font = f)
codebox.SetStyle((0,0), s)
The last line is what throws an error. Specifically:
TypeError: in method 'RichTextCtrl_GetStyle',
expected argument 3 of type 'wxTextAttrEx &'
I based my code on stuff I found online, however I immediately recognized that my version of wxPython was different because the tuple I supplied as a range in SetStyle() was instead supplied as two different arguments, however when I imitated that setup the interpreter threw me an error and asked me to use a tuple.
Does anyone know where I'm going wrong? Or, of course, a workaround? Thanks.

s should be of type wx.TextAttrEx, not wx.TextAttr like you have done.
SetStyle(self, range, style)
Set the style for the text in range to style
Parameters:
range
(type=RichTextRange)
style
(type=TextAttrEx)

Related

What does 'arg1 must be a string, bytes or code object' mean? exec()

an image of the error message for anyone interested
trying to make a simple version of a language translator to a language I made that is similar to one from Outer wilds. but when I try to execute one of the letters for turtle to draw it says as the image above or
arg1 must be a string, bytes or code object
As I am not an amazing coder, I have no clue what that means.
This is the code that I have written and I want to exec()
There is another image containing the code I want to exec()
if anyone can tell me how to solve this thanks! and if you are able to use more simple python and dumb the answer down an little bit so I can understand then that would also be massively helpful!
if you can't dumb it down for me, no problem!
a = T.fd(20)
b = T.fd(20); T.rt(60); T.fd(20)
Neither of those statements does what you think they do. The first statement calls the fd function immediately (presumably moving the turtle forward by 20), and returns None, so None will be stored in a.
The second line also does a forward 20 and stores that return value in b. It then does a right 60 and a forward 20, but nothing about those will be stored in b. Those are completely separate statements.
The bottom line is, if you want to store up a macro to be executed later with exec, then those lines MUST BE STRINGS. You don't have any strings. So, change your code to:
a = "T.fd(20)"
b = "T.fd(20); T.rt(60); T.fd(20)"
...etc...
Then it will do what you want. This is not the BEST way to do this, because exec is a bad habit, but it will do what you want. The better way would be to do something like:
a = "F20"
b = "F20,R60,F20"
and then write a little interpreter to convert those to turtle movements.

can i define k elem in random.sample function as an variable?

I am begginer in Python. And what I am trying to do is make a random text from the list generator, in which you can choose how many things from the list you want to print out. When im trying to make k=var in which var=Entry(...). It gives me this error: TypeError: '<=' not supported between instances of 'int' and 'NoneType'. If you can give me tips to improve my skills and knowledge Id be really happy.
k determines how many items from the list I wanna print out:
def nahodny_generator():
list = ["more", "more1", "more3"]
sampling = random.sample(list, k=2)
oklbl = Label(root, text=sampling)
oklbl.grid(row=6, column=0)
I got your code into a state where I was able to run it and I didn't see any errors:
import random
from tkinter import Label, Widget
from typing import Optional
root: Optional[Widget] = None
def nahodny_generator() -> None:
words = ["more", "more1", "more3"]
sampling = random.sample(words, k=2)
oklbl = Label(root, text=sampling)
oklbl.grid(row=6, column=0)
nahodny_generator() # no exceptions raised?
One probable bug I noticed is that sampling is a List[str] (note that list is the name of Python's list class, and you probably don't want to name your own variables that) and you're passing it as a parameter called text, which I would assume expects a str (tkinter doesn't have type declarations though so it's not obvious that's the case).
Nothing in the code you shared uses the <= operator so the specific error you saw is coming from some other piece of it. When you get an error, the message will include the exact file and line number that it came from; you can use that to narrow down the source of the bug.
The function nahodny_generator is referenced in Button command, and I want to push that button with the entry inside and print out the amount of things in a list determined by the entry. Also I am using tkinter GUI and if you could give me some tips where to continue after overcoming the beginner level I would be very happy.

Getting a selection in 3ds Max into a list in Python

I am writing in Python, sometimes calling certain aspects of maxscript and I have gotten most of the basics to work. However, I still don't understand FPValues. I don't even understand while looking through the examples and the max help site how to get anything meaningful out of them. For example:
import MaxPlus as MP
import pymxs
MPEval = MP.Core.EvalMAXScript
objectList = []
def addBtnCheck():
select = MPEval('''GetCurrentSelection()''')
objectList.append(select)
print(objectList)
MPEval('''
try (destroyDialog unnamedRollout) catch()
rollout unnamedRollout "Centered" width:262 height:350
(
button 'addBtn' "Add Selection to List" pos:[16,24] width:88 height:38
align:#left
on 'addBtn' pressed do
(
python.Execute "addBtnCheck()"
)
)
''')
MP.Core.EvalMAXScript('''createDialog unnamedRollout''')
(I hope I got the indentation right, pretty new at this)
In the above code I successfully spawned my rollout, and used a button press to call a python function and then I try to put the selection of a group of objects in a variable that I can control through python.
The objectList print gives me this:
[<MaxPlus.FPValue; proxy of <Swig Object of type 'Autodesk::Max::FPValue *' at 0x00000000846E5F00> >]
When used on a selection of two objects. While I would like the object names, their positions, etc!
If anybody can point me in the right direction, or explain FPValues and how to use them like I am an actual five year old, I would be eternally grateful!
Where to start, to me the main issue seems to be the way you're approaching it:
why use MaxPlus at all, that's an low-level SDK wrapper as unpythonic (and incomplete) as it gets
why call maxscript from python for things that can be done in python (getCurrentSelection)
why use maxscript to create UI, you're in python, use pySide
if you can do it in maxscript, why would you do it in python in the first place? Aside from faster math ops, most of the scene operations will be orders of magnitude slower in python. And if you want, you can import and use python modules in maxscript, too.
import MaxPlus as MP
import pymxs
mySel = mp.SelectionManager.Nodes
objectList = []
for each in mySel:
x = each.Name
objectList.append(x)
print objectList
The easiest way I know is with the
my_selection = rt.selection
command...
However, I've found it works a little better for me to throw it into a list() function as well so I can get it as a Python list instead of a MAXscript array. This isn't required but some things get weird when using the default return from rt.selection.
my_selection = list(rt.selection)
Once you have the objects in a list you can just access its attributes by looking up what its called for MAXscript.
for obj in my_selection:
print(obj.name)

Assign string to QLineEdit with PySide/PyQt

I'm having a little bit of trouble assigning values to a QLineEdit. I've read the documentation and feel that the QLineEdit.SetText() command will be used at some point.
I've used Qt Designer to design a GUI for my software. On the main window (MainWindow.py, with an accompanying ui_MainWindow.py setup file), I have a LineEdit (lineEditScanBarcode) which has strong focus. I've managed to pull input from that LineEdit pretty well. What I'd like to do is this:
If the input in LineEditScanBarcode = x, then assign the name 'John Smith' to a secondary QLineEdit (lineEditUser) which has a zero focus policy. This is what I have so far:
def ScanBarcode(self):
barcode = self.lineEditScanBarcode.text()
self.lineEditScanBarcode.clear()
if barcode == '12345':
print("Welcome John")
self.lineEditUser.setText() = 'John'
else: print("Sorry, user not recognised.")
Upon running this, I get the following error:
Syntax Error: can't assign to function call
I've had a look at the above error, but I'm still unsure as to what's going on here. I still have no idea to open one window on top of another (this software package will have about 10 windows), but that's another story!
Is my logic here on track? I've never used Qt before, so my understanding of the intricacies involved is lacking to say the least.
Any input would be great!
As the comment states, the error is on this line:
self.lineEditUser.setText() = 'John'
You are attempting to assign the value 'John' to that functioncall (as the error states). If you review the documentation for QLineEdit in PyQT, you'll see that QLineEdit.setText() requires a string to be passed to it.
So, what you need to do instead is pass the value 'John' to the function like so:
self.lineEditUser.setText('John')
On another note your idea that your
software package will have about 10 windows
is definitely something that you want to reexamine. More windows, especially when undocked and floating independently will no doubt cause usability issues. I'd strongly recommend sharing your ideas over at UserExperience.SE.

Python - wxGrid.DeleteCols() : Segmentation Fault

I am working with xyPython, specifically a wx.Grid and when I attempt to delete a column from the grid the program crashes and terminal says "Segmentation Error"
dataGrid.CreateGrid(30, 20)
...
dataGrid.DeleteCols()
That is pretty much the code. I can delete rows, just not columns.
If I remove the delete column line it works fine.
According to the following thread, you may have to set the column labels yourself or you could get an error:
https://groups.google.com/forum/?fromgroups=#!topic/wxpython-users/IpARv4wVoqw
Then in this newer thread, it's said that you may have to call IncRef: https://groups.google.com/forum/?fromgroups=#!topic/wxpython-users/I1kndNvIEEQ
http://www.wxpython.org/docs/api/wx.grid.Grid-class.html
there are parameters for DeleteCols(self, pos, numCols, updateLabels)

Categories

Resources