Python: Get caret position [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to get the caret position in Python. I tried using win32gui.GetCaretPos() but it always returns 0,0.
Do you have any ideas how to make it work?
Thanks
Chris

If the caret is in a window created by another thread, you need to call AttachThreadInput. Assuming you want the caret of the foreground window, you can get to it like this:
import win32gui
import win32process
import win32api
fg_win = win32gui.GetForegroundWindow()
fg_thread, fg_process = win32process.GetWindowThreadProcessId(fg_win)
current_thread = win32api.GetCurrentThreadId()
win32process.AttachThreadInput(current_thread, fg_thread, True)
try:
print win32gui.GetCaretPos()
finally:
win32process.AttachThreadInput(current_thread, fg_thread, False) #detach

Related

Can you please solve python_3 attribute error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Attribute Error: partially initialized module 'turtle' has no attribute 'Turtle' (most likely due to a circular import)
starting code-->
import turtle
from turtle import *
s = Screen()
s.screensize(700, 1000)
speed(5)
def myPosition(x, y):
penup()
goto(x, y)
pendown()
pensize(2)
ending code-->
myshirt()
allhands()
mymouth()
alleyebrows()
alleyes()
ht()
input()
Assumption: Your file is named turtle.py. When you do import turtle, it tries to import itself. Try renaming it.

Why is tkinter <Enter> and <Leave> event not working? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to get my widget to light up when the mouse is hovering over it. Here is what I have tried:
self.buttons = []
self.buttonTips = ["Select and Add a clip to the project", "Order the clips in your project", "Trim the selected clip",
"Cut out audio breaks in the selected clip", "Preview clip (in new window)", "Render/Export your video"]
self.commands = [self.getClipPath, self.reorderClips, self.trimClip, self.cutOutAudio, self._preview, self.finishVideo]
self.images = [_addImage, _switchImage, _trimImage, _autoTrimImage, _previewImage, _exportImage]
for index, tip in enumerate(self.buttonTips):
self.buttons.append(Button(self.root, image=self.images[index], command=self.commands[index], bg=_bgcolor, relief=FLAT))
self.buttons[index].photo = self.images[index]
self.buttons[index].place(x=index * 30, y=490)
self.buttons[index].bind("<Enter>", func=partial(changeButtonBG, 1, self.buttons[index]))
self.buttons[index].bind("<Leave>", func=partial(changeButtonBG, 0, self.buttons[index]))
addToolTip(self.buttons[index], tip)
When I change the event type to <Motion> the function runs perfectly, but when I use or it doesn't work? Anyone know why?
Ok I have figured out my problem. In this line here:
addToolTip(self.buttons[index], tip)
the addToolTip() function also adds a bind to the widget. So I edited the binds so they had:
button.bind(func=function, add="+")
which adding the add="+" made it work

How to solve "Name error" in Python tkinter programme? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
from tkinter import *
root = Tk()
mylabel = label(root, text = "What's up?")
mylabel.pack()
root.mainloop()
This is the code I used in Visual code studio. And it shows me an error:
NameError: name 'label' is not defined
What do I need to change to make this work?
pls change your code like this
mylabel = Label(root,text = '')
you should use Lable instead label

Python application launch position (location) on monitors [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have been given a project in python and I'm not too familiar with it but I understand the basics due other coding work I've done.
The task is to basically make the application launch in last location it was closed.
There is no save button and no config (xml / ini etc.) currently used so I would assume it would need to create one of these, if not existing, or update an existing one upon closure of the window.
Everyone has different size monitors / monitor layouts and resolutions so is there a module that can assess this and save the X,Y co-ords per user configuration?
Or is there a better way to do this using the modules listed below? Or do I need to import an additional module?
These are the current modules imported:
import os
import sys
import pygtk
pygtk.require('2.0')
import gtk
Any help would be greatly appreciated.
UPDATE:
Forgot to post an update:
I managed to get this working perfectly into the existing app using J Arun Mani method.
I used os module to define the local directory where the file should be written to / read from and boom it works flawless. Thanks again
You can use the methods, Gtk.Window.get_position and Gtk.Window.move to get and set coordinates of window. (Link to doc)
But be aware that the placement of windows is users' preference and is taken care by window manager. So normally, you should not mess with it.
A simple example to demonstrate what you wanted:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
def window_start(win):
try:
fp = open("win_coords")
except FileNotFoundError:
print("No file !")
return
coords = fp.read()
x, y = eval(coords) # Don't use eval if you can't trust the file
print("Moving to ", coords)
win.move(x, y)
fp.close()
def window_close(win, event):
fp = open("win_coords", "w")
coords = tuple(win.get_position())
print("Writing coordinates ", coords)
fp.write(str(coords))
fp.close()
win.destroy()
Gtk.main_quit()
label = Gtk.Label(label="Hello world")
win = Gtk.Window()
win.add(label)
win.connect("delete-event", window_close) # Connect to get the coordinates
win.show_all()
window_start(win)
Gtk.main()

TypeError: descriptor 'fbind' requires a 'kivy._event.EventDispatcher' object but received a 'str' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have just started to use kivy. Currently, I am watching videos on kivy and copying their code. I copied it but got an error while the dude in the video didnt. I am trying to make a moving Label and a textinput box.
This is my code:
b = BoxLayout()
t = TextInput
f = FloatLayout()
s = Scatter()
l = Label(text="hell0")
f.add_widget(s)
s.add_widget(l)
b.add_widget(f)
b.add_widget(t)
this is the error im getting:
TypeError: descriptor 'fbind' requires a 'kivy._event.EventDispatcher' object but received a 'str'
You are doing a t = TextInput which does not create a TextInput widget, so your b.add_widget(t) fails because t is not a widget. Just change t = TextInput to t = TextInput(). And if you are using GridLayout, you must specify either cols or rows in the call to GridLayout().

Categories

Resources