How to copy file contents to clipboard - python-3.x

So basically i want to copy the contents of this file to my clipboard
Ive tried many things like using different modules and such but for some reason i just cant do it
import shutil
owl_hub = open('owlhub.txt','r')
if a == "Owl Hub":
lbl.config(text = owl_hub.readlines())
shutil.copy(owlhub.readlines())

Use tkinter; it comes with python and provides a cross-platform way to change the clipboard
import tkinter as tk
root = tk.Tk()
root.withdraw()
root.clipboard_clear()
with open('owlhub.txt') as f:
root.clipboard_append(f.read().strip())
root.update()
root.destroy()

Related

Using Python to control system audio i [Voice Control]

I'm writing a program to control my system volume either increase or decrease in voice command. but I don't know how and what are all the packages should to be installed? I'm using python 3.7.0 in pycharm
I am not sure but u can probably use tkinter. I had created a duplication GUI file so just copy this code and paste in python:
from tkinter import *
try:
root=Tk()
root.title("file copier")
fname=mystring=StringVar()
root.geometry = Canvas(root, width = 3000, height = 3000)
label=Label(root,text="enter the file name with file extension", fg="black") .grid(row=0, column=0,sticky=W)
entry=Entry(root, textvariable=fname) .grid(row=0, column=1)
#specifying the function going to be assigned to the button
def duplicatefunction():
print(fname.get())
with open(fname.get(),"rb") as f:
data = f.read()
with open("copy file(rename this file and copy info inside).exe","wb") as f:
f.write(data)
button1=Button(text="duplicate file", command=duplicatefunction) .grid(row=0, column=2)
root.mainloop()
except FileNotFoundError:
print("no such file found named", entry)
so if u see i have typed exe extension after file name in the function. Try typing all ur code in a notepad and then convert to exe from this file(after pasting the code save the file and run it), u can do that from changing extension but u should make copy too, anyway this file's code was only for ur reference and btw the copy file will be in pycharm only, if u use it for python IDLE like me it will come in file explorer. convert a notepad file from this by duplicating the file and then go back, type ur code for the volume and use this code at the end for the code to control volume:
fname=StringVar()
print(fname.get())
with open(fname.get(),"rb") as f:
data = f.read()
so the data should be read and work, i hope it does cuz it works for me, in other projects.
I found something interesting on https://techoverflow.net/2020/04/04/how-to-set-windows-audio-volume-using-python/
but this only works for Windows.
The package they are using is pycaw. You can install it with
pip install pycaw.
That is the script on the website:
from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume
import math
# Get default audio device using PyCAW
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(
IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
# Get current volume
currentVolumeDb = volume.GetMasterVolumeLevel()
volume.SetMasterVolumeLevel(currentVolumeDb - 6.0, None)
# NOTE: -6.0 dB = half volume !
If I use
volume.SetMasterVolumeLevel(-65.25, None)
my system volume is set to 0
and with
volume.SetMasterVolumeLevel(0, None)
I can set the system volume to 100

Converting Python 2.7 to Python 3 with tkinter

I have a working application using Python 2.7 and Tkinter that uses these constructs:
from Tkinter import *
import Tkinter
import tkFileDialog
class Window(Frame):
#...
# other functional code
#...
def ChangeCWD(self): #CWD is current working directory
root = Tkinter.Tk()
root.withdraw()
directory = tkFileDialog.askdirectory( ... )
root = Tk()
root.mainloop()
It has labels, buttons, canvas, multiple frames and file dialogue boxes and it all works nicely.
I have begun updating the code to work on Python 3.5 and, so far all functions seem to work except for the file dialog. This is where I have got to so far:
from tkinter import *
import tkinter
import tkinter.filedialog
class Window(Frame):
#...
# other functional code
#...
def ChangeCWD(self): #CWD is current working directory
root = tkinter.Tk()
root.withdraw()
directory = filedialog.askdirectory( ... )
root = Tk()
root.mainloop()
However this code produces the error
"NameError: name 'filedialog' is not defined"
when the filedialog.askdirectory() statement is reached. Could anyone provide any help to understand what I should do to correct the situation please?
As an aside, please be gentle with me! I've always been rather mystified by the various ways of invoking import statements and how to use "tk." or "root." before some function calls. There are simply too many conflicting explanations out on the web that I can't get a clear picture.
You use import tkinter.filedialog, which imports tkinter.filedialog with the namespace tkinter.filedialog, then you try to use filedialog in your code.
Pick one of these two:
change your call to tkinter.filedialog.askdirectory( ... )
change your import to import filedialog from tkinter, which will import tkinter.filedialog with the namespace filedialog.
Note: from tkinter import * might seem like it should import filedialog, but that * does not import submodules unless the package has explicitly specified that they should.

Trying to convert python file to .exe (contains tkinter code)

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.

filedialog using external initial directory

Is there a way to use an 'initialdir' option of tkinter filedialog to point an external directories?
I see that it works fine for local lirectories (C:\Program Files...)
but fails for something external (ftp://1.2.3.4 ...)
If not, do you have any alternatives for getting the file path from external ftp server using some GUI filedialog?
This is what I have now:
import tkinter as tk
from tkinter import *
from tkinter import filedialog as fd
root = tk.Tk()
def location():
filename = fd.askopenfilename(initialdir = "ftp://1.2.3.4")
print(filename)
b = Button(root, text="GetFile", command=location)
b.grid(column=0, row=15, sticky='EW')
root.mainloop()
Thanks!
No. Not unless the underlying OS supports mounting an FTP as an external drive. The tkinter filedialog class uses the python OS module which doesn't directly support FTP.
You would have to write your own filedialog with FTP support or find one that someone has already written.

Alternative for tkinter's askopenfilename

Currently I am using tkinter's askopenfilename in a quicklist editor for Ubuntu to get a file's name and location. Although it works fine, the look and feel is not native.
Is there an easy alternative dialogue window, to navigate and get a file's name and location?
You could try with wxPython FileDialog:
>>> import wx
>>> d = wx.FileDialog(None)
>>> d.ShowModal()
5101
>>>
It gives a more OS specific look
wxPython is arriving soon to py3k as the Phoenix project and there are already snapshots for windows and mac (see my comment below). If you want something more stable you can use pyQt QtGui.QFileDialog.
import sys
from PyQt4 import QtGui
class Dialog(QtGui.QMainWindow):
def __init__(self):
super(Example, self).__init__()
filename = QtGui.QFileDialog.getOpenFileName()
print filename
app = QtGui.QApplication(sys.argv)
dialog = Dialog()
You have a more complete example here.
Zenity
Zenity's File Selection Dialog provides an easy and natively looking solution with the --file-selection option. The dialog provides a number of options.
See also Zenity's man pages.
In its simplest form:
#!/usr/bin/env python3
import subprocess
try:
file = subprocess.check_output(["zenity", "--file-selection"]).decode("utf-8").strip()
print(file)
except subprocess.CalledProcessError:
pass
Gtk's FileChooserDialog
Another option is Gtk's FileChooserDialog, which produces, as one might expect, perfectly natively looking file chooser dialog windows.

Resources