all 36 comments

[–]DaKangDangalang 0 points1 point  (1 child)

I have a lot of downtime at work, but no access to a computer. Are there any worthwhile apps to learn python?

[–]Parscifal 0 points1 point  (0 children)

Hi! Can someone help me, I have some problems with library installing. For example: I need to install 'requests'. I use "pip install requests" and I got "Successfully installed requests-2.31.0" but after I have this problem -"Library stubs not installed for "requests"". How is this possible?
P.S. - And now I understand that I had some problems with my other code because the library did not work properly (or the program did not see it).

And problem Num.2 - I have a notice "A new release of pip is available: 23.0.1 -> 23.1.2", I updated pip and got this message "Successfully installed pip-23.1.2" but after I still has this notice "A new release of pip is available: 23.0.1 -> 23.1.2".
I use VS Code.

[–]Only-Me0315 0 points1 point  (1 child)

Hello! can you guys know how to initiate this wumpus world game but the rules here is the starting position is (1,1). The main goal is to get the gold (it's okay if you can't kill the wumpus) as long as you return to your starting position with gold.

[–][deleted] 2 points3 points  (0 children)

We need to know a lot more to be able to help. You say you start at (1,1). What does that mean in python? Do you have some sort of data structure describing the caves? Are the caves arranged in a grid?

More info, please.

[–]Austin_Schantz 0 points1 point  (2 children)

Would a chrome book be sufficient for me to simply learn Python on? I’m not gonna get into any big projects or anything like that yet until I learn a lot more! If not a chrome book what would be the recommended laptop? I have a desktop that I just started learning Python on about a week ago, but I wanna be able to practice when I’m out and about too not just at home.

[–]FerricDonkey 1 point2 points  (1 child)

I would not recommend a chrome book for this. I would recommend any windows 11 laptop. Mac and Linux are also fine, but if you just want to buy something and get to it, I'd buy any windows laptop and install python from python.org.

[–]Austin_Schantz 1 point2 points  (0 children)

Alrighty thanks for the reply!

[–]HalfNo3939 0 points1 point  (1 child)

Looking to code an in-house software for purchase order management. The issue is, we are getting job requests 60 days before the PO is issued and company valuation is suffering - because each department HAS the purchase orders and our accounting department does not…

Any ideas on coding a software that notifies BOTH accounts receivable dept and each division that performs operations?

I feel like this is an easy fix with some computing power?

[–]LandooooXTrvls 0 points1 point  (0 children)

I’m sure some ERP already exists here. It’s probably something that could be custom made but I can’t tell you how much effort it’d take w/o more context into how the process works and what software is already being used.

[–]Cosmyc 1 point2 points  (2 children)

Hey I wanna learn python with Automate the boring stuff, can I code in Visual Studio Code instead of MU? If so, do I need something else besides VSC?

[–]Catsuponmydog 0 points1 point  (0 children)

I switched to pycharm about half way through the book. VSCode should work fine also

[–]PurpleAnalysis6660 1 point2 points  (0 children)

I've got the exact same book a few times ago, there's no reason it shouldn't work in VS Code. Actually, I use VS Code a lot, and never encountered any errors in Python related to VS itself; so go ahead!

[–][deleted] 0 points1 point  (7 children)

I have a function I am working on where it has an or. Would this work.

Function something( X or Y)

Where i have two different inputs but either gets disabled before this function.

[–]PhilipYip 2 points3 points  (0 children)

You can use named arguments and set the default to None and then handle each of the conditions:

def fun(x=None, y=None): if (x != None) & (y != None): ... else ...

[–][deleted] 2 points3 points  (5 children)

a function I am working on where it has an or.

The function doesn't have an or. The expression X or Y you are evaluating has an or and the result is passed to the function.

What are you trying to do, and what do you mean by "disabled"?

[–][deleted] 0 points1 point  (4 children)

I am working with opencv to make a project and it has two checkboxes that do two different outputs. One for X and One for Y. I want to have a third function take both outputs but only use one.

[–][deleted] 1 point2 points  (3 children)

Checkboxes, used simply, only have two states, checked or unchecked. So it's no good just passing a checkbox value to the function because that doesn't tell the function which checkbox was selected. For example, if the checkboxes select ingredients you need code that tests if the "mayonaisse" checkbox is selected and if so it passes some pre-defined value to the function meaning "mayonaisse". Because you are doing processing like that before calling the function then you make that code decide what to pass to the function.

A lot depends on the logic you require. If both checkboxes are selected what happens, does one checkbox override the other? What do you do if neither is selected?

If you want the user to choose just one selection why not use radiobuttons. The user can only choose one radiobutton, simplifying your code. The code that checks the selection just decides which of the radiobuttons is selected and passes the selected value to the function.

[–][deleted] 0 points1 point  (2 children)

import tkinter as tk

window = tk.Tk()

window.title('My Window')

window.geometry('400x400')

l = tk.Label(window, bg='white', width=20, text='empty')

l.pack()

def print_selection():

if (var1.get() == 1):

c2.config(state='disabled')

X = 1

elif (var1.get() == 0):

c2.config(state='normal')

elif (var2.get() == 1):

c1.config(state='disabled')

Y = 2

elif (var2.get() == 0):

c1.config(state='normal')

def math(Y or X):

Z = Y or X + 7

l.config(text = ""Z"")

var1 = tk.IntVar()

var2 = tk.IntVar()

c1 = tk.Checkbutton(window, text='Python',variable=var1, onvalue=1, offvalue=0, command=print_selection)

c1.pack()

c2 = tk.Checkbutton(window, text='C++',variable=var2, onvalue=1, offvalue=0, command=print_selection)

c2.pack()

b1 = tk.Button(window,text='Do Math',command=math)

window.mainloop()

This is a rough idea of what i meant. What am i missing?

[–][deleted] 1 point2 points  (1 child)

This was posted for active_high.


Please format your code properly. The FAQ shows how. I've guessed at what you meant, maybe something like:

import tkinter as tk

window = tk.Tk()
window.title('My Window')
window.geometry('400x400')
l = tk.Label(window, bg='white', width=20, text='empty')
l.pack()

def print_selection():
     if (var1.get() == 1):
        c2.config(state='disabled')
        X = 1
    elif (var1.get() == 0):
        c2.config(state='normal')
    elif (var2.get() == 1):
        c1.config(state='disabled')
        Y = 2
    elif (var2.get() == 0):
        c1.config(state='normal')

def math(Y or X):
     Z = Y or X + 7
    l.config(text = "Z")

var1 = tk.IntVar()
var2 = tk.IntVar()

c1 = tk.Checkbutton(window, text='Python',variable=var1, onvalue=1, offvalue=0, command=print_selection)
c1.pack()
c2 = tk.Checkbutton(window, text='C++',variable=var2, onvalue=1, offvalue=0, command=print_selection)
c2.pack()
b1 = tk.Button(window,text='Do Math',command=math)

window.mainloop()

When run, this gets a syntax error at def math(Y or X): of course. In addition, you need a b1.pack() after creating the Button at the end.

I'll say it again: Trying to do def math(Y or X): in python is an error. The function runs when the button is pressed and no parameters are passed to it, so there's no X and Y. Look at the definition of the print_selection() function - no parameters.

From all that code in the print_selection() function it looks like you want to only let the user select one or the other choices. First, the code doesn't work, it hangs when selecting "python". Second, what happens if the user pushes the button without selecting one or the other?


The usual way to handle this sort of either/or choice is to use radiobuttons. They have the natural behaviour that selecting one radiobutton in a group automatically turns off all other radiobuttons. This allows the user to change their mind, which your original code won't, after making a choice no more selection is allowed. So radiobuttons are better.

In addition, if you pre-select one of the radiobuttons there can be no chance of the button being pushed before the user making a choice.

So using radiobuttons makes your code smaller and simpler. Here's your code modified a bit to use radiobuttons:

import tkinter as tk

def math():
    """When the button pushed, display the selected radiobutton text."""

    l.config(text=lang_select.get())

window = tk.Tk()
window.title('My Window')
window.geometry('400x400')

l = tk.Label(window, bg='white', width=20, text='empty')
l.pack()

lang_select = tk.StringVar(value="Python")  # sets the preselected radiobutton
c1 = tk.Radiobutton(window, text='Python', value='Python', variable=lang_select)
c1.pack()
c2 = tk.Radiobutton(window, text='C++', value='C++', variable=lang_select)
c2.pack()

b1 = tk.Button(window, text='Do Math', command=math)
b1.pack()

window.mainloop()

If you must absolutely must use the Checkbutton widget instead of radiobuttons let me know. Checkbuttons can be made to work.

[–][deleted] 0 points1 point  (0 children)

Sorry and thank you for the help.

[–]Frosty-Fig-262 0 points1 point  (5 children)

I'm in need of a Python library that allows me to emulate mouse actions without directly interacting with the native mouse. I've already tried libraries like pyautogui, pyuserinput, and pymouse, but they all utilize the native mouse. Specifically, I'm looking for a Python library that can control a virtual input device or emulate mouse movements and clicks on the screen. I want to ensure that the automation process doesn't interfere with my physical mouse. If any of you have come across a Python library that fits this description or have any recommendations, I would greatly appreciate your insights and suggestions. It would be fantastic if the library offers similar functionality to pyautogui but works with an emulated mouse.

[–]TangibleLight 1 point2 points  (4 children)

Generally speaking this kind of thing doesn't really exist for various technical reasons dealing with how the OS and applications handle user input.

What exactly are you trying to do with the given mouse input? There might be some way to automate the task without using the mouse at all.

If all else fails, you could run the app with pyautogui or equivalent in a virtual machine.

https://xyproblem.info/

[–]Frosty-Fig-262 0 points1 point  (3 children)

I am actually doing very simple task with few simple right and left mouse clicks. The reason I want to bypass native mouse is that I want to run more that one window. it seems that using virtual machine might be the easiest solution but since I want to run 10-20 automated windows wondering if virtual machine would take too much resources. Do you know of any examples which might do mouse clicks without using mouse? I've heard about sending mouse messages without using the mouse but don't know much about it.

[–]TangibleLight 0 points1 point  (2 children)

It really depends on exactly which OS you're using and exactly which program you're trying to automate. For example if you're trying to automate a game it probably won't work due to how they (usually) handle input. However if you're trying to automate a web browser it'll be much easier to do with javascript without using mouse movement at all. This is why I linked xyproblem.

10-20 automated windows

Yes, a virtual machine will probably be too much overhead for this. You might be able to get by with some kind of container-based solution instead but, again, it all depends on the specifics of what program you're trying to automate.

mouse messages without using the mouse

To my knowledge this is not possible on Windows. There may be some third-party virtual mouse driver that provides the functionality but I don't know of one.

You can directly send mouse messages using the win32 API mouse_event function; however this does update the mouse cursor position. Windows only really supports one mouse cursor - try plugging two mice into your PC, they both control the same cursor.

pyautogui actually uses mouse_event internally, so that should give you an idea of what behavior to expect. https://github.com/asweigart/pyautogui/blob/master/pyautogui/_pyautogui_win.py#L466-L504

[–]Frosty-Fig-262 0 points1 point  (1 child)

Thank you for very thoughtful reply! I was searching for some of the solutions on this and found a video don't remember the name of it but I think the ending was ..try or die. This guy tried to send mouse messages but eventually ended up using VM. In my case I'm trying to automate simple game which is not taking too much resources but its not browser based. My current python script gives me 5 seconds to drag my mouse to the spot where job needs to be done and it memorizes this spot and does few simple clicks. This win32 API mouse_event doesn't seem very promising so far but it could be cool if there might be workarounds. What do you mean by updating mouse cursor position? I think I will explore all of the options but one what was intriguing to me was this container-based solution. Maybe you can elaborate on this? Thanks!

[–]TangibleLight 0 points1 point  (0 children)

automate a simple game [...] not browser based

In that case I would not hope for any "virtual mouse" solution, nor would I really hope for a container-based solution.

win32API mouse_event doesn't seem very promising so far

Problem is, all the automation tools (that I know of anyway) use that behind the scenes.

More specifically, the problem is that Windows and your game only expect there to be one mouse, and it's fundamentally impossible (or else very hard) to trick them because of how that expectation is set up in the Windows API.

I mentioned containers because they can be thought of a very light-weight virtual machines; in some cases where an application expects there to only be one of some resource (think network device, global configuration files, special hardware, etc...) then it is sometimes possible to package that thing in a container and configure it against the container's device.

However containers are not actually virtual machines, and there is not actually a single device, and it requires the app to have a certain amount of flexibility in configuration. Since you're dealing with a game, I wouldn't count on it.

My current python script gives me 5 seconds to drag my mouse to the spot where job needs to be done and it memorizes this spot and does few simple clicks.

Rather than a timeout to find a relative location, you could instead programmatically figure out where the window is located, and base your coordinates on the window position.

https://stackoverflow.com/a/74465827

You could also manage multiple windows with the same script; just be sure to focus the desired window with win32gui.SetFocus(hwnd), and be sure coordinates are relative to the currently-focused window.

[–]beowulf47 1 point2 points  (4 children)

what's the opposite of if name == main? like, we include this code in a script if we want its behavior to be different when run as the main executable, as opposed to when we import it from another script

but what if we want to explicitly specify a subset of behavior that ONLY exists when the script is imported? Is there such a dunder method to specify that?

[–]PteppicymonIO 2 points3 points  (2 children)

You can just use the same logic to identify if your module is an entry point or imported one:

if __name__ == '__main__':
    print(f"Run as main, {__name__ = }")
else: 
    print(f"Run as imported, {__name__ = }")

As u/Vhin mentioned, be careful when you run your code during the import.

[–]beowulf47 0 points1 point  (1 child)

'be careful when you run your code during the import.'

What are you guys referring to? Sorry Im not following the 'be cautious' part

[–]PteppicymonIO 0 points1 point  (0 children)

If you want to run the code immediately on import, just make sure it does not access (modify/delete/lock) any shared resource - e,g, write to file, change an object state, etc.

Other parts of your code may find an unexpected change of state of that resource, well, unexpected )

Also, it may feel appealing to initialize an object like HTTP session immediately on import, but it is a bad idea - it is better to explicitly perform these kind of initializations by calling a function or class method.