I'm pretty new to Python, so bear with me on this. I looked at the possible duplicate of my question, and wasn't able to get it to work for me. I'm assuming I just don't understand the answer code enough, but I can't seem to get the terminal command I want, just the one used in the answer.
The answer I'm referring to can be found here: Giving a command in a embedded terminal
I've got a simple GUI that I've embedded a terminal into. The purpose of this is to run tcpdump to capture packets. I wrote a simple script that did this just fine, but I'm looking to make it easier to use on a touch screen, hence the GUI.
I set up a button that runs the following code:
os.system('sudo tcpdump')
But it doesn't execute in the embedded terminal.
Could someone explain how to make this command run in the terminal that's embedded in the GUI?
Here's all the code I have for the GUI so far:
from tkinter import *
import os
class PcapGUI:
def __init__(self, master):
self.master = master
master.title("Packet Captures")
self.start_button = Button(master, text="Start", command=self.start)
self.termf = Frame(root, height=400, width=500)
self.wid = self.termf.winfo_id()
os.system('xterm -into %d -geometry 70x20 -sb &' % self.wid)
def start(self):
os.system('sudo tcpdump')
root = Tk()
gui = PcapGUI(root)
root.mainloop()
Any help is appreciated!
Related
I am trying to create a python text editor. What I want to create in my app, is a textbox where the user writes python code, another box where the user will see the terminal output and a button to run the code. After a few days of research, I found out that terminal output can be shown by using the subprocess module. I used it, worked pretty well! I type print("hello world"), it works.
However, only one thing that did not work is when we run something like input("Enter your name: "). The program just crashes when I run it. This is where I got stuck for a long time. I know doing something like that is not easy because the output box is just a text widget. But, I have seen that many apps can do that so the input function might be possible.
The code below is a sample from my project:
from tkinter import *
import subprocess, os
def run():
code = open(f"{os.getcwd()}/test2.py", 'w')
code.write(input_box.get(1.0, END))
code.close()
process = subprocess.Popen("python3 test2.py", stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
output, error = process.communicate()
output_box.delete(1.0, END)
output_box.insert(END, output)
output_box.insert(END, error)
root = Tk()
input_box = Text(root)
input_box.grid(row=0, column=0)
output_box = Text(root)
output_box.grid(row=0, column=1)
btn = Button(root, text="Run", command=run)
btn.grid(row=1, column=0)
output_box.insert(1.0, "Output:\n")
output_box.bind('<BackSpace>', lambda _: 'break')
root.mainloop()
The program crashes here:
My current approach is to make my own input function hidden in the users code (this line of code does it): code.write(open(f"{os.getcwd()}/input_string.py", 'r').read(), input_box.get(1.0, END))
Here is input_string.py where it uses tkinter.simpledialog.askstring:
from tkinter import simpledialog
def input(prompt):
ipt = simpledialog.askstring('input', prompt)
return ipt
This works but I am open to better solutions here. I hope you understand my question and the effort I put into this.
I’m building a program in python that will copy large files using robocopy. Since the gui freezes while the copy is done i only have two options:
1. Learn how to do multithreading and design the gui to show the progress and not freeze.
2. Keep the console on after compiling with pyinstaller as an alternative to show robocopy progress while the gui freezes.
I am open to doing multithreading but i’m a beginner and is pretty hard to understand how to make another subprocess for robocopy and from there extract the progress into a label from gui. The option i thought about is to have the cmd console active only while the copy is done. Is it possible? The scenario will be like this:
Open the program (the console will be hidden)
Press the copy button (console pops up and shows the copy progress while the gui freezes)
After the copy is done hide the console again
As i said above. I’m not totally excluding adding multithreading but for that i will need some help.
Thanks!
Please try this code, should be working, please let me know if something wrong:
import tkinter as tk
import os
import subprocess
import threading
main = tk.Tk()
main.title('Title')
frame_main = tk.Frame(main)
frame_main.grid(columnspan=1)
src = 'D:/path/to/the/folder'
dest = 'D:/path/to/the/folder2'
selection_platf = len(os.name)
def copy_build_button():
if selection_platf < 11:
subprocess.call(["robocopy", src, dest, r"/XF", 'BT V_SyncPackage.zip', "/S"])
else: #for linux
subprocess.call(["robocopy", src, dest, "/S"])
def copy_thread():
thread_1 = threading.Thread(target=copy_build_button)
thread_1.start()
button_main1 = tk.Button(frame_main, text="copy_build_button", width=50, height=5, fg="green", command=copy_thread)
button_main1.grid(column=0, sticky='N'+'S'+'E'+'W')
main.mainloop()
First of all, thank you for trying to help me out. I am currently programming my first GUI with tkinter and i try to create buttons with big fonts, cause i want to create a program for visually impaired people. Sadly I have two problems i can't solve with the internet on my own ..
This is the relevant part of my code: (sorry for the german variables)
import tkinter
from tkinter import *
from tkinter import font
import tkinter.messagebox
class Oberflaeche(tkinter.Frame):
def __init__(self, master=None):
tkinter.Frame.__init__(self, master)
self.pack()
MyFont = font.Font(family='times', size=50)
self.close_window = tkinter.Button(self, font=MyFont, text="Programm \nbeenden", command=self.close_window, bg="white", height = 3, width = 18, bd=3, relief="solid")
self.close_window.pack()
def close_window(self):
root.destroy()
root = tkinter.Tk()
root.title("Prototyp MVP")
root.minsize(width=300, height=300)
root.configure(background='white')
oberflaeche = Oberflaeche(master=root)
oberflaeche.mainloop()
when I try to change the font tkFont.Font is not working. There is the Error:
NameError: name 'tkFont' is not defined
That is the reason I tryed font.Font. But no matter how I change the font family or type, it always looks awful and pixelated....
Picture of the failed Button
I am using python 3.5.5, Ubuntu 16.04 and tk 8.6.8.
I am using Spyder(python 3.6) here is the result;
i.hizliresim.com/6NgZlW.jpg
Maybe because of version?
Actually your program is working succesfully ı did not change any codes ı tried on my pc and the result is perfect(Win10 Spyder(Python 3.6).)
i am also used to Jupyter notebook and i was trying with VS Code everything is working well. I suggest you trying to play around with scaling and let me know is it working for you?
Scaling of Tkinter GUI in 4k (3840*2160) resolution
Results
Jupyter
VS Code
I'm currently building an console application in python that does a lot of calculations on input gotten from the user and i've seen several apps that have gui windows but the input is still gotten from the console and this is exactly what i want, i'm currently learning tkinter so i wanted to ask is it possible to do this in tkinter because the tutorial i have on tkinter dosen't talk about including consoles in gui's here is a screenshot of what i mean https://www.imageupload.co.uk/image/ZEXe though in the screenshot the command line just prints out what is going on in the program but i want my main application to be in that area, so i can get input from the user and then the results can be printed out on the gui. Answers can also suggest other python gui frameworks. Thanks
Tkinter can accept text inputs and return values based on what is put in. As far as I know there is no actual command line input function. You could always try implementing it yourself. I think this is very customizable so I'm not going to write it myself but these are some of the things u might use:
textbox = Entry(root)
textbox.pack()
textbox.config(bg="red") #this would change the background to black. fg for text color
textarea = Text(root) #this creates a big text area
#textbox.get() will return the value of the textbook
Also, you could embed a full terminal into a tkinter window by using xterm (you might need to install it first if it's not natively available on your machine.
from tkinter import *
import os
root = Tk()
termf = Frame(root, height=500, width=500)
termf.pack(fill=BOTH, expand=YES)
wid = termf.winfo_id()
os.system('xterm -into %d -geometry 100x100 -sb &' % wid)
root.mainloop()
This creates a new tkinter window (500 x 500) and them embeds an xterm window into it (100 x 100)
I looked through a tutorial on using Tkinter and saw that the following code:
>>> from Tkinter import *
>>> win=Tk()
This should produce a box with the title Tk and nothing else. However, when I try this code out no such box appears. I'm not getting any errors so I suspect it's working as intended. Is it possible that there are additional steps I have to take if I'm on a mac?
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
This code runs automatically, however, in the guide it suggests that I use $ python hello1.py to run this code, which doesn't work. Any ideas on why this might be?
However, this larger block does not work:
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button = Button(
frame, text="QUIT", fg="red", command=frame.quit
)
self.button.pack(side=LEFT)
self.hi_there = Button(frame, text="Hello", command=self.say_hi)
self.hi_there.pack(side=LEFT)
def say_hi(self):
print "hi there, everyone!"
root = Tk()
app = App(root)
root.mainloop()
root.destroy() # optional; see description below
The issue seems to have something to do with mainloop but I'm confused because at the same time that earlier block worked just fine with a root.mainloop() part.
Do you run this code in IDLE?
Try above code in terminal (not in IDLE), then it will work as expected.
So if you want to try and run it in Terminal you should follow the steps below
note- 'I find that running programs is Terminal that involve a tkinter Gui will often crash for me, however it may work for you'
1st - Open Terminal
2nd - Type 'python3.4' then press space bar once
3rd - Open a Finder window
4th - Go to where you saved your python file in the Finder window
5th - Once you have located the file in Finder, drag the file into the Terminal window
6th - Press enter, and enjoy your python program.
another note - 'It sounds like you need a better Python IDE, you should try out PyCharm it is a great Python IDE which you can code and run python programs in including tkinter stuff'
You can download PyCharm here https://www.jetbrains.com/pycharm/