Visual Studio Console Window Location - c++

Is it possible to make the console window in Visual Studio show up in the same location on screen for each compile?
I always have to move it around whenever I compile because its starting location overlaps things.

By default you can change console window settings per application and windows will save them for the next time when this application is run. You may change the start location by clicking right button on the console title bar, then choosing Properties and Layout tab. Then uncheck the "Let system position window" checkbox and type the coordinates you would like.
Unfortunately Visual Studio when you run an application without the debugger (Ctrl + F5) will launch the cmd.exe /c <your app> command. So changing properties on this window will also change settings for all console sessions in the system.
This does not apply to the debug run as under the debugger VS launches just the application and so the settings apply only to its console windows.

I came here with the same problem, and was sure there must be an option in VS. I ended up writing a quick python script that runs in the background, searches for the window and sets its last position. Also optionally sets it to always on top, and persists the position in a file. Hopefully will help someone else out.
import win32api
import win32gui
import win32con
import msvcrt
import sys
import time
import threading
import pywintypes
import argparse
import re
import weakref
import pickle
def log_message(message,*args):
print(("[%s]" % message).ljust(30) ," ... ",*(str(arg) for arg in args))
def make_file_name(arguments):
return arguments.save_file_name or "window_" + re.sub("[^a-z]","_",arguments.title_text,flags=re.IGNORECASE)
def update_proc(flags,arguments):
found_pos = (0,0,500,500)
found_hwnd = -1
if arguments.save_to_file and not arguments.reset:
load_file_name = make_file_name(arguments)
try:
with open(load_file_name,"rb") as load_file:
found_pos = pickle.load(load_file)
except:pass
while not flags.exit:
try:
hwnd = win32gui.FindWindow(arguments.class_text,arguments.title_text)
if not hwnd: continue
if hwnd != found_hwnd:
log_message("new window found",hwnd)
for _ in range(10):
win32gui.SetWindowPos(hwnd,0 if not arguments.on_top else win32con.HWND_TOPMOST,*found_pos,0)
time.sleep(0.01)
log_message("setting window position",found_pos)
found_hwnd = hwnd
time.sleep(0.1)
if win32gui.IsIconic(found_hwnd):
continue
rect = win32gui.GetWindowRect(found_hwnd)
new_pos = (rect[0],rect[1],rect[2]-rect[0],rect[3]-rect[1])
#ignore negative pos
if True in [ r < 0 for r in new_pos[0:2] ]:
continue
if new_pos == found_pos:
continue
if arguments.save_to_file:
log_message("saving position",new_pos)
save_file_name = make_file_name(arguments)
try:
with open(save_file_name,"wb") as save_file:
pickle.dump(new_pos,save_file)
except:pass
else:
log_message("updading stored position",new_pos)
found_pos = new_pos
except pywintypes.error:pass
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-s","--savetofile",help="Save window settings to a file",action="store_true",dest="save_to_file")
parser.add_argument("-sfn","--savefilename",type=str,help="File name to use if --savetofile specified (or a default is generated)",dest="save_file_name")
parser.add_argument("-ot","--ontop",help="Window Always Ontop",dest="on_top",action="store_true")
parser.add_argument("-c","--class",type=str,help="Window Class Name",dest="class_text")
parser.add_argument("-r","--reset",help="Reset Window Position",action="store_true")
parser.add_argument("-t","--title",type=str,help="Window Title Text",required=True,dest="title_text")
arguments = parser.parse_args()
flags = type("", (), {"exit":False})()
update_thread = threading.Thread(target=update_proc,args=[weakref.proxy(flags),arguments])
update_thread.start()
if arguments.save_to_file:
log_message("saving to file",make_file_name(arguments))
while True:
if flags.exit: break
key = msvcrt.getch()
if key == b'q':
flags.exit = True
log_message("exiting")
break
update_thread.join()

Related

Error ALDialog Python Nao

I have a problem when using the ALDialog module on Python IDE and to load on Nao. I tried in different ways to load a dialogue but I always fall back on the same error.Runtimeerror LoadTopic::ALDialogIncorrect file myDialog.topIn the first case I write directly the text that I save in a. top file but at the time of LoadTopic () I have an error.In the second case I want to load the. top file by giving it the path. I come back to the same mistake again.Do you have a solution to my problem?Thank you very much.
import qi
import argparse
import os
import sys
from naoqi import ALProxy
def main(robot_ip, robot_port):
dialog = """
topic: ~myTopic() \n
language: enu \n
u:(test) hello \n """
file = open("myDialog.top","w")
file.write(dialog)
file.close()
# load topic
proxy = ALProxy("ALDialog",robot_ip,robot_port)
proxy.setLanguage("English")
self.topic = proxy.loadTopic("myDialog.top")
# start dialog
proxy.subscribe("myModule")
# activate dialog
proxy.activateTopic(self.topic)
if name == "main":
parser = argparse.ArgumentParser()
parser.add_argument("--ip", type=str,
default="169.254.245.164",help="Robot's IP address : '169.254.245.164'")
parser.add_argument("--port", type=int, default=9559,help="port number, the default value is OK in most cases")
args = parser.parse_args()
main(args.ip, args.port)
ALDialog.loadTopic expects an absolute filepath on the robot - it doesn't know anything about the context from which you're calling it (it could be from another computer, in which case of course it can't open that file). You need to be sure that your .top is indeed on the robot, and pass it's absolute path to ALDialog.
Once installed on the robot this path will be something like /home/nao/.local/share/PackageManager/apps/your-package-id/your-dialog-name/your-dialog-name_enu.top

pygame program exits with no error message when run with pythonw

I'm trying to run a pygame program using pythonw to avoid having the console window show up. This causes a weird issue related to print statements.
Basically, the program will just exit after a few seconds with no error message. The more printing I do, the faster it happens.
If I run it in idle or at the command prompt (or in linux) the program works fine. This problem only happens when launched with pythonw (right-click, Open With, pythonw).
I'm using python 2.7.11 on Windows XP 32-bit. pygame 1.9.1release.
Is there a workaround for this? Why does the program simply terminate with no error?
import pygame
from pygame.locals import *
succeeded, failed = pygame.init()
display_surface = pygame.display.set_mode((320, 240))
clock = pygame.time.Clock()
terminate = False
while terminate is False:
for event in pygame.event.get():
if event.type == QUIT:
terminate = True
area = display_surface.fill((0,100,0))
pygame.display.flip()
elapsed = clock.tick(20)
print str(elapsed)*20
pygame.quit()
You don't need to remove print statements. Save them for later debugging. ;-)
Two steps to solve this problem:
Firstly, keep all the code in py file - don't change it to pyw now; Say it is actualCode.py
Then, create a new file runAs.pyw with the following lines in it
# In runAs.pyw file, we will first send stdout to StringIO so that it is not printed
import sys # access to stdout
import StringIO # StringIO implements a file like class without the need of disc
sys.stdout = StringIO.StringIO() # sends stdout to StringIO (not printed anymore)
import actualCode # or whatever the name of your file is, see further details below
Note that, just importing actualCode runs the file, so, in actualCode.py you should not enclose the code which is executed, in what I call is it main running file condition. For example,
# In actualCode.py file
....
....
....
if __name__ == '__main__': # Don't use this condition; it evaluates to false when imported
... # These lines won't be executed when this file is imported,
... # So, keep these lines outside
# Note: The file in your question, as it is, is fine

How to open a python program within python

I'm working on a program using tkinter (2.7) that when a button is clicked, it opens a separate python program. What I have been trying to do is give the button a command and define it as the separate program. This is what I have so far:
from Tkinter import *
from ttk import *
import os
app = Tk()
app.title("iClassics")
app.geometry("450x300+200+200")
#Definitions
def mTetris():
subprocess.Popen("Tetris.py")
#Heading
headlabel = Label(text="iClassics", font=("Times", 30), background=("blue")).pack()
#Buttons
buttontetris = Button(app, text="Tetris", command=mTetris).pack()
buttonpong = Button(app, text="Pong").pack()
buttonbrick = Button(app, text="Brick Breaker").pack()
buttonsnake = Button(app, text = "Snake").pack()
app.mainloop()
Why can't I open my tetris.py on click?
Take a look at popen.
You shoold call it this way:
subprocess.Popen(["python", "tetris.py"])

python 2.7 while loop, ttk.progressbar not working

I am working on a progress bar that keeps track of a certain function in pygame.
The following code causes a loop that I must force quit. And I can not figure our my error. Any help would be great.
from Tkinter import *
import ttk
import sys
import pygame
myGui = Tk()
myGui.title("Progress Bar")
myGui.geometry("400x200+200+300")
value_progress = StringVar()
pygame.mixer.init()
pygame.mixer.music.load("/home/david/Documents/aaa.mp3")
pygame.mixer.music.play()
def position():
global value_progress
while pygame.mixer.music.get_busy() == True:
value_progress.set(float(pygame.mixer.music.get_pos()))
b = Button(myGui, text="Start", )
b.pack()
p = ttk.Progressbar(myGui, variable=value_progress,
mode='determinate', length=350,
maximum= 512920)
p.pack()
I call the function from the shell. and then it stalls and does not come out of it.
This is only the progress bar portion of my work. However, it causes the program to crash every time.
Do not loop. Instead try following code:
def position():
global value_progress
if pygame.mixer.music.get_busy():
value_progress.set(float(pygame.mixer.music.get_pos()))
myGui.after(100, position)
Tk.after(ms, f) call f after specified ms millisecond.

Call current PyMOL session from python script

I'm trying to call current PyMOL session from python script (wxpython GUI), and then load some data from PyMOL and send few commands to PyMOL. At the moment I can open a new PyMOL session in python script:
import sys, os
from wx import *
app = wx.App()
dialog = wx.FileDialog ( None, message = 'Set PyMOL directiry', style = wx.OPEN)
if dialog.ShowModal() == wx.ID_OK:
# Pymol path
moddir = dialog.GetDirectory()
sys.path.insert(0, moddir)
os.environ['PYMOL_PATH'] = moddir
import pymol
pymol.finish_launching()
else:
print 'Nothing was selected.'
from pymol import *
dialog1 = wx.FileDialog ( None, message = 'Set PDB file', style = wx.OPEN)
if dialog1.ShowModal() == wx.ID_OK:
pdbfile = dialog1.GetPath()
cmd.load(pdbfile)
else:
print 'Nothing was selected.'
dialog.Destroy()
app.MainLoop()
BUT actually I'd like to check in my python scrip whether any PyMOL session is already opened.
I found discussion corresponding to this topic here:
Only call function if PyMOL running
Following this discussion I tried to call 'print' function in PyMOL:
try:
from pymol import cmd
print 'Connected'
except:
<open new Pymol sesssion>
but I do not see any text in PyMOL cmd. I tried to determine PyMOL path before this statement and again I failed.
Does anyone know how to call current PyMOL session from python script?
To my knowledge, there is no way to interact with an existing PyMOL session from the Python interpreter. There are a few alternatives:
The existing PyMOL command prompt accepts valid Python and you can run any script directly from the prompt. Perhaps you could execute your script from there instead?
You can start up a new PyMOL session as part of your script. PyMOL's -i flag may be especially helpful- it enables you to work in a headless environment:
-i Disable the internal OpenGL GUI (object list, menus, etc.)
Take a careful look at PyMOL's __init.py__. You'll find the intricacies of PyMOL's threading there. Perhaps you can find something useful to manipulate?
Side note:
Calling print from your script will not make the text appear in the PyMOL session, it will merely write the text to standard output (i.e. it will be printed to your terminal).
The following PyMOL wiki pages may be of help to you:
Running scripts
Launching PyMOL
Launching from a script