Binding a button to a VPython 7 function - python

I am currently trying to make a physics simulation using VPython on my laptop. I want to make a GUI so that when the client presses the run simulation button, it opens chrome and the simulation runs, after taking the user's input from entry boxes. However, when I run the source code, chrome automatically opens and I want it to open only when the user presses the run simulation button. I tried using the lambda method to bind the function to the button, but it isn't working. Here is a very simplified version of what I am trying to do:
import tkinter as tk
from vpython import *
### Simulation ###
def run_simulation(r):
sphere(radius=r)
### GUI ###
root = tk.Tk()
text_variable = tk.StringVar()
entry = tk.Entry(root, textvariable=text_variable, width=10)
entry.pack()
button = tk.Button(root, text='Run Simulation', command= lambda: run_simulation(eval(text_variable.get())))
button.pack()
root.mainloop()
In short, if you run this code, it will open chrome automatically (even though I used lambda), but I only want it to do so if the button is clicked. Thanks in advance.

Here are a bunch of vpython demo programs that you can run in a browser using glowscript vpython
http://www.glowscript.org/#/user/GlowScriptDemos/folder/Examples/
Just click run beside the image and it runs the simulation.
These same vpthon Demo programs are also found on github here.
https://github.com/BruceSherwood/vpython-jupyter/tree/master/Demos_no_notebook
you can run the programs from the Windows Command prompt if you have python 3.6 installed on your machine. Just type
python BinaryStar.py
and it will open a web browser and run the BinaryStar vpython demo program. You can do the same for the other demo programs in the above directory on github.
Maybe you can get your button to run the command "python myVpythonProgram.py" to run the vpython program you want to run in a browser.

Related

Tkinter opens instantly subprocess instead of waiting for button click [duplicate]

This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 7 months ago.
I'm working on a code editor based on nano. I've decided to make GUI for it, and the GUI language I chose is Python. I'm trying to make a button that opens batch files on Tkinter. I did so by importing import subprocess. Now, I created a button that opens the batch file with subprocess:
ttk.Button(frm, text="New Session", command=subprocess.run(['csession.bat']) ).grid(column=0, row=1)
Little problem is, when I launch the file, the subprocess action instantly gets executed instead of waiting for the user to click. I'm using VSCode and also debugging in VSCode (if that does any difference).
you need to use lambda if you want to define the command this way.
you can also create a function and set the command to run it.
import tkinter as tk
from tkinter import ttk
import subprocess
def runBat():
subprocess.run(['foo.bat'])
root = tk.Tk()
ttk.Button(root,text="text",command= lambda: subprocess.run(['foo.bat'])).pack()
ttk.Button(root,text="text2",command=runBat).pack()
root.mainloop()

I'm struggling with being able to close a simple widget using tkinter

I have some Python experience and wish to learn about GUI development using tkinter.
I can create a simple widget, but when I try to close the window, things "hang" and nothing happens. Only by restarting the Python kernel can I get the window to close.
I'm running Python 3.7 using Spyder and, based upon some simple examples I've found in other forums tried the following:
import tkinter
root = tkinter.Tk()
root.title("Hello!")
simple_label = tkinter.Label(root, text="Easy, right?")
closing_button = tkinter.Button(root, text="Close window",
command=root.destroy)
simple_label.pack()
closing_button.pack()
root.mainloop()
As I mention above, the window does not close when I click my mouse on the Close Window button. I just get the "swirly" indicator on my Mac indicating the program is not responding. However, I am able to perform calculations in the Spyder console.
The code works fine, but what you should do is open the program in the Python IDLE that comes when you download Python. When you run the program it should open up the Python Launcher, which allows you to interact your your GUI (And it will let you close click the close button) I am using a Mac and everything works fine.
I was analyzing your case and we BOTH did something we didn't notice after.
When you use the "root.destroy" command, you should add parenthesis after.. Had the same thing happening to me
Added root.destroy ( ) and worked flawlessly. <-- Use it without SPACES
Hope it helps someone in the future!

I'm having problems using python on a mac: (Running a program without the idle and Background of tkinter buttons)

I've been having problems running my python program on the mac.
Opening without the IDLE:
Firstly, whenever I open the program it opens with the IDLE by default. I would like it to open with the launcher, but when I tried opening it with the launcher, it came up with a preferences window that had no "Ok", "Next", or "Continue" button.
On Windows, if you open a python program, then by default it will run it with the launcher, causing it to go directly into the program without the user needing to go through any other windows. The .pyw extension can also be used to make it run without the console. How would I set the Mac so that it does this?
Background of tkinter buttons:
In the following function I have a menu with several buttons:
def MainMenu():
Move() # This clears any widgets that are on the screen.
Win.unbind("<Up>") # Win is the name of my window.
Win.unbind("<Down>")
Win.unbind("<Return>")
Win.config(bg="#FFFF00")
try:Welcome=GUI.Label(Win,text="Welcome "+Name+("."if Name[-1]!="."else"")+"\nYou should have £"+str(Cash)+("0"if str(Cash)[-2]=="."else"")+".\n What would you like to do?",bg="#FFFF00") # GUI is the variable containing the tkinter library.
except IndexError:Welcome=GUI.Label(Win,text="Welcome "+Name+".\nYou should have £"+str(Cash)+".\n What would you like to do?",bg="#FFFF00")
Welcome.place(x=0,y=0)
ManualButton=GUI.Button(Win,text="Manual adjustment.",width=96,height=4,command=ManualChange,bg="#FFFF00")
ManualButton.place(x=0,y=0)
RecButton=GUI.Button(Win,text="Recall.",width=96,height=4,command=Recall,bg="#00FF00")
RecButton.place(x=0,y=0)
Setti=GUI.Button(Win,text="Settings",command=Settings,bg="#FFFF00",width=96,height=4)
Setti.place(x=4,y=4)
if Set:
RecoButton=GUI.Button(Win,text="Record spending.",width=96,height=4,command=Spending,bg="#FF80C0")
RecoButton.place(x=256,y=14)
Win.update_idletasks()
Welcome.place(x=Win.winfo_width()//2-Welcome.winfo_width()//2,y=16)
ManualButton.place(x=Win.winfo_width()//2-ManualButton.winfo_width()//2,y=256)
RecButton.place(x=Win.winfo_width()//2-ManualButton.winfo_width()//2,y=192)
if Set:RecoButton.place(x=Win.winfo_width()//2-ManualButton.winfo_width()//2,y=128)
Setti.place(x=Win.winfo_width()//2-ManualButton.winfo_width()//2,y=340)
Win.update()
Here is a picture of the menu it creates (Name="S", Cost=8805.33, Set=True):
This is what it looks like on a Mac (Name="S", Cost=96, Set=True):
I would like to make the buttons look more like they do on Windows, or at least get rid of the grey space around the buttons. Does anyone have any idea how I'd do this?

Python script tkinter resolution and screenshot size effected when running pythonw.exe

I currently have a python script which launches a tkinter GUI instance, with a button that once clicked takes a screenshot.
When I run this script under python.exe, the tkinter resolution is fine and the screenshot captures the whole screen. However, when using pythonw.exe, the tkinter window resolution changes (button gets bigger for example) and the screenshot only captures a portion of the screen - the top left hand corner normally.
I need to use pythonw.exe in order to prevent the console window appearing.
Does anyone know why the tkinter window resolution and the screenshot capture is being effected? Presumably the effect on the resolution is why the screenshot capture is being reduced as well.
I am fairly new to Python, so any help with this would be greatly appreciated, below is snippet of the code for the tkinter window and the screenshot functionality. To reiterate this functionality runs completely fine under python.exe.
The screenshot functionality using ImageGrab:
callback1():
ImageGrab.grab_to_file('test.png')
The tkinter window:
master = Tk()
master.wm_attributes("-topmost", 1)
master.title("Report")
master.configure(background='white')
master.iconbitmap(default='icon.ico')
master.resizable(0, 0)
frame1 = Frame(master, bg='white')
frame1.pack(side=BOTTOM, fill=X)
button1 = Button(frame1, compound=BOTTOM, width=307, height=82,
image=photo1, bg='white', command=callback1)
button1.pack(side=TOP, padx=2, pady=8)
I have now fixed this. It seems it was related to the compatibility settings for pythonw.exe in Windows. Changing the following fixes the image capture, but also the tkinter window resolution:
Go to your python directory (c:/python27/ for me)
Right click python.exe and select properties
Select the compatibility tab
Press the "Change settings for all users" button
Check the "Disable display scaling on high DPI settings" box
Credited by this post:
Python Imaging Library fails to grab whole screen
Hopefully this helps someone with the same issues. It does beg the question as to how this can be done automatically, as for users of a python application it is not user-friendly for them to have to change these settings.

having trouble clicking in program - pyautogui

I'm trying to click into a program window using pyautogui.
When clicking in the program window, on a button I wish to press, a loading icon appears next to the mouse cursor as if it is thinking, and it never actually clicks. It does, however, move to the cursor location that I provide.
Here's the code
import pyautogui
pyautogui.doubleClick(x=300, y=300)
I opened Excel to see if it will click into that, and it does, but only if I run the code as:
import pyautogui
pyautogui.doubleClick(x=300, y=300)
pyautogui.doubleClick(x=300, y=300)
What the heck do I do?
If the application in running as administrator and pyautogui script is not running as administrators, then control will not work. pyautogui script should also run as administrator to control it
Try giving the interval argument
pyautogui.doubleClick(x=300, y=300, interval=0.25)

Categories

Resources