Trying to convert my code to .exe file so that other users can make use of it.
The purpose of this code is to get an interface where the user can enable/disable LAN connection in a single click.
Kindly 'run as administrator' you IDLE to make this code work.
import tkinter as tk
import subprocess as sub
import os
WINDOW_SIZE = "350x100"
root = tk.Tk()
root.title("Windows Adapter Toggler")
root.geometry(WINDOW_SIZE)
#to execute bash command right from python script
tk.Button(root, text="DISABLE Internet", command=lambda: sub.call('netsh
interface set interface name="Ethernet" admin=DISABLED')).pack()
tk.Button(root, text="ENABLE Internet", command=lambda: sub.call('netsh
interface set interface name="Ethernet" admin=ENABLED')).pack()
#os.system(bashCommand)
root.mainloop()
SideNote:
1.Will work if your ethernet port name is 'ethernet',if something else then change the name in the code.Will implement this 'changing name of port'feature later in the code.
2.Already tried converting with pyinstaller and py2exe but didn't happen.
Related
I have a function to display a GUI window where the user can type the password. I use IDLE shell a lot during development, so using getpass is not an option.
This function is part of a utility script that has other functions I want to be able to use (after submitting the password using this function).
import tkinter
def password_from_user():
password = None
def get_password(*__):
nonlocal password
password = entry.get()
window.destroy()
window = tkinter.Tk()
entry = tkinter.Entry(window, width=40, show='*')
entry.bind('<Return>', get_password)
entry.pack()
entry.focus_set()
window.mainloop()
return password
The password gets stored and returned from the function. However, the Tkinter window stays open. I can press ⌘+Tab to switch back to IDLE shell, but it's slightly inconvenient. I'd like this window to be completely closed.
If I run this as a script, of course, everything gets closed at the end:
if __name__ == '__main__':
print(password_from_user())
But, I need this function only for IDLE session. If I were only using command line, I would use getpass which would be more than sufficient.
Edit 1: I have tried using both destroy and quit. Neither of them works for me.
Edit 2: Just tested on a Windows machine with Python 3.8.5. It works. So, I'm pretty sure it's something to do with macOS.
Figured out a solution that satisfies me.
Save the Tkinter code in a separate module (say, _password_box_ui.py):
import tkinter
password = None
def get_password(*__):
global password
password = entry.get()
window.quit()
window = tkinter.Tk()
window.title('Password')
entry = tkinter.Entry(window, width=40, show='*')
entry.bind('<Return>', get_password)
entry.pack()
entry.focus_set()
window.mainloop()
print(password)
Then, in another module (which I'll import in the IDLE shell), use a function like this:
import pathlib
import subprocess
import sys
def password_from_user():
process = subprocess.run(
[
f'{sys.base_exec_prefix}/bin/python',
pathlib.Path(__file__).parent / '_password_box_ui.py',
],
capture_output=True,
)
return process.stdout.rstrip(b'\n').decode()
May not be very elegant, but serves my purpose, which is to use IDLE to get password from the user using a GUI (so that, ultimately, no password needs to be stored in plaintext anywhere).
I am building a very simple python project that will allow the user to read a message in a box and then click OK. This will part of a larger program.
The issue is that when I run it in Spyder, the message box ends up behind all other windows. How do I make it be the focus and open in front of all other windows? I am running a Windows 11 computer.
I call the below function from another python script.
# import libraries
# sys / os to change working directory
import sys
import os
# easygui for message boxes
from easygui import *
# Create function to set working directory
def setworkdir():
uni_code = fileopenbox()
print(uni_code)
# Create Welcome Box
def openMsgBox():
msgbox("Welcome to DocPhoto. Click on Ok to get started")
setworkdir()
The script to call it:
from docFunctions import openMsgBox
openMsgBox()
I have seen answers where the programmer is trying to do this in tkinter, but I'm not using tkinter. Is there a way to do it without tkinter?
I have a problem with Gtk.Dialog in Python GTK4.
I want to display a dialog that the process is running and immediately after that to start the os.system() module. Unfortunately, the dialog is displayed and the os.system module is not started. Can you advise me how to make when I display Gtk.Dialog() to start the os.system() module?
import os
# I am sending only part of the source code
def on_buttonCreate_clicked(self, widget, *args):
self.dialog()
def dialog(self)
dialog_b = Dialog_building(self)
response_b = dialog_b.run()
self.build()
dialog_b.destroy()
def build(self):
os.system("cat /proc/cpuinfo")
I am writing a script in kivymd to toggle an LED which is connected to a microcontroller. The script is supposed to send a socket UDP msg to the MCU server, which includes a command to toggle the LED. The script works fine on windows, and the building with buildozer is successful (although not shown in this script but I tried another version to simply print hello world to an MDLabel and it worked) but whenever I press the btn_led on Android, the application shuts down. Any ideas why this is happening and how to fix it?
from kivymd.app import MDApp
from kivymd.uix.button import MDFlatButton
from kivy.uix.screenmanager import Screen
import socket
class TableApp(MDApp):
def build(self):
# Add Widgets
screen = Screen()
btn_led = MDFlatButton(text="Toggle LED", on_release=self.toggle_led,
pos_hint={"center_x": 0.5, "center_y": 0.5})
screen.add_widget(btn_led)
return screen
def toggle_led(self, event):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto(b"LED", ("10.0.0.167", 1111))
s.close()
TableApp().run()
I just learned that the buildozer.spec file contains permissions which can be configured to include INTERNET. I uncommented the permissions line which contains INTERNET by default and everything worked like a charm!
The problem I am encountering is that I appear to be stuck in an infinite loop, (If I am not, please correct me). I am using tkinter for python 3.6 (64 bit) on windows 10.
In the module I am having an issue with I have 3 entry widgets and 2 buttons. Both buttons call the "destroy()" function in order to kill the parent window.
Below is a heavily abstracted version of my module, the purpose of the module is to take inputs from the entry widget and write them to a file.
def Create():
parent = Tk()
parent.MakeItlookNice
entry1 = Entry(parent)
entry1.insert(INSERT, "Please enter your desired username here")
entry2 = Entry(parent)
entry2.insert(INSERT, "Please enter your desired password here")
entry3 = Entry(parent)
entry3.insert(INSERT, "What is your mother's maiden name")
Submit = tk.Button(parent,
text ="Click here to submit your stuff",
command = lambda: [parent.destroy(),
submit.function()])
Cancel = tk.Button(parent,
text ="Click here to cancel your request",
command = lambda: parent.destroy())
parent.mainloop()
This function is contained within the module "RegisterNewUser". The "Menu" module is the module that called this function. As far as I am aware once parent.destroy() is called there is no more code to execute since it is all contained within parent.mainloop(), therefore the function is finished and the "Menu" module should continue executing.
What should happen:
I want the Submit button to destroy the window, execute the function and then return to the "Menu" module.
I want the cancel button to destroy the window and return to the "Menu" module.
What actually happens:
The window closes, like it is supposed to
But the code inside the "Menu" module does not start executing again
When I go to close the python shell, it warns me that the program is still running
Ultimately my question is, what code is still running and why hasn't it stopped?
Thank you for reading this and if you require more detail please let me know.
EDIT: I have done research on this topic before posting this question. I have read the documentation on both the tk.destroy() function and the tk.mainloop() function, I have also opened up the Tkinter module in IDLE to try and understand what happens at a deeper level but after all this, I was still unable to figure out a solution. This is my first question on stack overflow, please forgive me if I have done anything wrong.
Hmmm, so you say multiple windows? an easier way to achieve a complex UI as such is using a concept called frames. Tkinter allows you to completely change you screen and layout if you switch to a new frames. This might require you to reprogram alot of code. for an example see Switch between two frames in tkinter
Also, Some guy built a really nice Bitcoin monitoring app using tkinter and frames on youtube
I think you would probably benefit from using Toplevel() here.
I have taken the code you provided and added it to a class used to create the main window and to manage the pop up window.
I noticed a few things with you code.
Its obvoious you are importing tkinter twice like this:
import tkinter as tk
from tkinter import *
I can tell from how you have written your entry fields vs your buttons. Do not do this. Instead just used one or the other. I recommend just using import tkinter as tk.
You are using a function to create a new tkinter instance and judging by your question you all ready have a tkinter instance created for your menu. Instead of creating new Tk() instances you can use Toplevel() instead to open a new window that can inherit everything from the main window and should be easier to manage.
You do not really need to use lambda in this situation. I have also removed the lambda function and replaced with a simple command that will work here.
Take a look at the below code and let me know if you have any questions.
import tkinter as tk
class MyApp(tk.Frame):
def __init__(self, master, *args, **kwargs):
tk.Frame.__init__(self, master, *args, **kwargs)
self.master = master
self.master.title("Main MENU")
tk.Button(self.master, text="Open top level", command = self.create).pack()
def create(self):
self.top = tk.Toplevel(self.master)
entry1 = tk.Entry(self.top, width = 35)
entry1.pack()
entry1.insert(0, "Please enter your desired username here")
entry2 = tk.Entry(self.top, width = 35)
entry2.pack()
entry2.insert(0, "Please enter your desired password here")
entry3 = tk.Entry(self.top, width = 35)
entry3.pack()
entry3.insert(0, "What is your mother's maiden name")
tk.Button(self.top, text ="Click here to submit your stuff",
command = self.Submit).pack()
tk.Button(self.top, text ="Click here to cancel your request",
command = self.top.destroy).pack()
def Submit(self):
print("Do something here to submit data")
self.top.destroy()
if __name__ == "__main__":
root = tk.Tk()
app1 = MyApp(root)
tk.mainloop()
You can use toplevel() and its library function wait_window() just prior to (or instead of) your mainloop() and your problem will be solved
wait_window() mentioned above worked for me in the code below replacing popup.mainloop() with it, the mainloop() kept my code in an infinite loop upon the popup even after okay button was hit