How can I introduce an embedded terminal with tkinter? - python-3.x

I am trying to make a simple GUI with some basic options of GPG in order to make it easier for new people to use this tool. I need to place a terminal in my GUI, so that trough the GUI the user can click buttons which activate commands in the terminal. With this program people could get used to see the commands working and I avoid the problem of the introduction of the passwords. I want this program to be free software, so that if anyone who helps wants to complete it and share it, please feel free to doing so (well, I would like to see it working correctly firstxD)
I have searched this things but the only answer which looked useful for me does not work (when I click the button of send it simlpy does nothing). I leave here the link to the this other question and my code:
Link: Giving a command in a embedded terminal
My code:
from tkinter import *
import tkinter.ttk as ttk
import os
def cifrado():
archivo=filebox.get()
algoritmo=algo_selec.get()
orden='gpg -ca --cipher-algo '+str(algoritmo)+' '+str(archivo)
os.system(orden) #I need to run this on the terminal
window=Tk()
window.title('GPG Gui')
window.geometry("600x600")
notebook=ttk.Notebook(window)
notebook.pack(fill='both', expand='yes')
frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
notebook.add(frame1, text='Cifrados simétricos')
notebook.add(frame2, text='Cifrados asimétricos')
#Symmetric encryption part
filelabel=Label(frame1,text='Archivo: ')
filelabel.grid(row=1,column=1)
filebox=Entry(frame1)
filebox.grid(row=1,column=2)
algolabel=Label(frame1,text='Algoritmo: ')
algolabel.grid(row=2,column=1)
algo_selec=ttk.Combobox(frame1,values=["IDEA","3DES","CAST5","BLOWFISH","AES","AES192","AES256","TWOFISH","CAMELLIA128","CAMELLIA192","CAMELLIA256"]) #DESPLEGABLE
algo_selec.set("AES256")
algo_selec.configure(width=18)
algo_selec.grid(row=2,column=2)
keylabel=Label(frame1,text='Contraseña: ')
keylabel.grid(row=3,column=1)
keybox=Entry(frame1,show="*",width=20)
keybox.grid(row=3,column=2)
b1=Button(frame1,text="Cifrar",command=lambda:cifrado())
b1.grid(row=1,column=3)
#Well, I need to write here the code of the terminal and connect it to the 'cifrado()' function
#Asymmetric encryption
window.mainloop()

Related

Python - Mouse clicks/Keyboard Clicks on window in background

I wanted to create a metin2 bot, nothing complicated. I am new into that so i just started to look for tutorials on youtube to find something that would lead me to world of coding, how does it work, what language is used to write it ect. So I found a tutorial on youtube about open CV Learn Code By Gaming - OpenCV and thought that's awesome thing to start with.
I got to the point where I can easily detect the names of monsters/metins and wont have any problems in finding the right points to click. After detection started working I thought about automating clicking on the window to farm some items etc. Simple things like clicking e.g 50 pixels below the name to auto attack worked just fine but the problem was that I wanted to make more bots to maximalize farming. So when i started to add more clients I got a problem where e.g you have to hold down SPACE in one window to attack from horse and click on another window which was stopping attacking from horse. So I thought about finding some code that can basically send message directly to window without controlling a mouse or keyboard so you can run multiple bots in one time and each will do perfect meanwhile you can do anything else on pc because your mouse and keyboard aren't used.
Let's start from code I found and none worked for windows in background (even with administrator privileges). Pyautogui doesn't work in background (window has to be in foreground to be clicked on and it controls mouse so there is no point in using that.
From that code I learned that I need to find "window ID" to connect to it and send messages. When i print hWnd it shows the numbers in Terminal and code passes without any fails but does nothing except printing the window ID ( Parent Handle ). Ofc I pip installed pywin32
import win32gui
import win32api
import win32con
def click(x, y):
hWnd = win32gui.FindWindow(None, "Client")
print(hWnd)
lParam = win32api.MAKELONG(x, y)
win32api.SendMessage(hWnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam)
win32api.SendMessage(hWnd, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, lParam)
click(100, 100)
Than i found another code that looked similar but used different function so first i used
wdname = 'Client'
hwnd = win32gui.FindWindow(None, wdname) # parent handle
print(hwnd)
Which printed me a window ID that i used in parameters in function
def control_click(x, y, handle, button):
l_param = win32api.MAKELONG(x, y)
if button == 'left':
win32gui.PostMessage(handle, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, l_param)
win32gui.PostMessage(handle, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, l_param)
elif button == 'right':
win32gui.PostMessage(handle, win32con.WM_RBUTTONDOWN, 0, l_param)
win32gui.PostMessage(handle, win32con.WM_RBUTTONUP, 0, l_param)
control_click(200, 200, 329570, button ='left')
But it still did nothing but code passed clear
Anyone have any ideas about how to make clicks/keyboard clicks in python on window in background without taking controll of mouse/keyboard? If you have any experience in automating and know better ways to create automation for clicking ect. and want to share it please do. If that can be done in another programming language also share your toughts about that of course if you want to.
Windows 10 x64
Python used: 3.9.2
All codes i found in topics were out of date that s why i am asking for help there. Thanks in advance :)

How can i create a terminal like design using tkinter

I want to create a terminal like design using tkinter. I also want to include terminal like function where once you hit enter, you would not be able to change your previous lines of sentences. Is it even possible to create such UI design using tkinter?
An example of a terminal design may look like this:
According to my research, i have found an answer and link that may help you out
Firstly i would like you to try this code, this code takes the command "ipconfig" and displays the result in a new window, you can modify this code:-
import tkinter
import os
def get_info(arg):
x = Tkinter.StringVar()
x = tfield.get("linestart", "lineend") # gives an error 'bad text index "linestart"'
print (x)
root = tkinter.Tk()
tfield = tkinter.Text(root)
tfield.pack()
for line in os.popen("ipconfig", 'r'):
tfield.insert("end", line)
tfield.bind("<Return>", get_info)
root.mainloop()
And i have found a similar question on quora take a look at this
After asking for additional help by breaking down certain parts, I was able to get a solution from j_4321 post. Link: https://stackoverflow.com/a/63830645/11355351

How to configure tkinter buttons to seem more modern

Okay so I'm a teacher using tkinter to create gui's with my students. My mac users are fine with the look but in windows their programs leave something to be desired. I don't have access to PIP (my sys admin WONT open it up for the kids to use terminal) so I'm limited in my libraries.
Im (slowly) building up some configurations that i think would help them get the idea behind how to configure (I explain its like adding CSS to HTML - doens't (usually) change the function, it just looks better) any tips or examples i can pass on to them would be super helpful. Here is an example of a slider i turned into a tactile on/off switch.
import tkinter as tk
main = tk.Tk()
def on_off_slider_action(slider_val):
if int(slider_val): #using as "truthy"
on_off_slider.configure(troughcolor="#00FF00")
else:
on_off_slider.configure(troughcolor="#FF0000")
on_off_slider = tk.Scale(main, from_=0,
to=1,orient=tk.HORIZONTAL,length=50,showvalue=0,
command=on_off_slider_action,width=30)
on_off_slider.configure(bd=0)
on_off_slider.configure(relief=tk.FLAT)
on_off_slider.configure(sliderrelief=tk.FLAT)
on_off_slider.configure(bg="#999999", activebackground="#888888" )
on_off_slider.grid(row=0,column=0)
main.mainloop()
example switch off
example switch on

How to copy-paste data from an OS-running application with Python?

I need to write an application that basically focuses on a given Windows window title and copy-pastes data in a notepad. I've managed to achieve it with pygetwindow and pyautogui, but it's buggy:
import pygetwindow as gw
import pyautogui
# extract all titles and filter to specific one
all_titles = gw.getAllTitles()
titles = [title for title in all_titles if 'title' in title]
window = gw.getWindowsWithTitle(titles[0])[0].activate()
pyautogui.hotkey('ctrl', 'a')
pyautogui.hotkey('ctrl', 'c')
Using Spyder, I ocasionally get the following error when activating:
PyGetWindowException: Error code from Windows: 126 - The specified module could not be found.
Additionally, I would be interested in doing this process without affecting the user working on the machine. Activate basically makes the window pop to front. Moreover, it would be better to not be OS dependant, but I haven't found anything yet.
I've tried pywinauto but the SetFocus() method doesn't work (it's buggy, documented).
Is there any other method which would make the whole process invisible and easier?
Not sure if this will help
I am using pywinauto to set_focus
import pywinauto
import pygetwindow as gw
def focus_to_window(window_title=None):
window = gw.getWindowsWithTitle(window_title)[0]
if not window.isActive:
pywinauto.application.Application().connect(handle=window._hWnd).top_window().set_focus()

I want to embed python console in my tkinter window.. How can i do it?

I am making a text editor and want to add a feature of IDLE in my app. So i want an frame with python IDLE embedded in it with all menus and features which original python IDLE gives.
I looked in source of idle lib but cannot find a solution.
try:
import idlelib.pyshell
except ImportError:
# IDLE is not installed, but maybe pyshell is on sys.path:
from . import pyshell
import os
idledir = os.path.dirname(os.path.abspath(pyshell.__file__))
if idledir != os.getcwd():
# We're not in the IDLE directory, help the subprocess find run.py
pypath = os.environ.get('PYTHONPATH', '')
if pypath:
os.environ['PYTHONPATH'] = pypath + ':' + idledir
else:
os.environ['PYTHONPATH'] = idledir
pyshell.main()
else:
idlelib.pyshell.main()
This code is of pyshell.pyw found under idlelib folder in all python install
I searched the idle.pyw and found that it uses a program pyshell which is real shell. So how can i embed it.
I want a Tkinter frame with python IDLE shell embedded in it.Please give the code. Thanks in advance.
idlelib implements IDLE. While you are free to use it otherwise, it is private in the sense that code and interfaces can change in any release without the usual back-compatibility constraints. Import and use idlelib modules at your own rish.
Currently, a Shell window is a Toplevel with a Menu and a Frame. The latter has a Text and vertical Scrollbar. It is not possible to visually embed a Toplevel within a frame (or within another Toplevel or root = Tk()). top = Toplevel(myframe) works, but top cannot be placed, packed, or gridded within myframe.
I hope in the future to refactor editor.py and pyshell.py so as to separate the window with menu from the frame with scrollable text. The result should include embeddable EditorFrame and ShellFrame classes that have parent as an arguments. But that is in the future.
Currently, one can run IDLE from within python with import idlelib.idle. However, because this runs mainloop() (on its own root), it blocks and does not finish until all IDLE windows are closed. This may not be what one wants.
If having Shell run in a separate window is acceptable, one could extract from python.main the 10-20 lines needed to just run Shell. Some experimentation would be needed. If the main app uses tkinter, this function should take the app's root as an argument and not call mainloop().
Tcl having Tkcon.tcl . when each thread source (means run/exec) the Tkcon.tcl
each thread will pop up a Tk shell/Tk console/tkcon.tcl very good idea for debug. and print message individually by thread.
Python having idle.py ... and how to use it ? still finding out the example .
The are same Tk base . why can't find an suitable example? so far ... keep finding...

Resources