get output of the python code - python

I have ScrolledText in Tkiner application ,in which i am supposed to write the python code in it and i have one button,by pressing that i send those lines of code to InteractiveInterpreter().runcode("code as a string")
This runcode() method return the NoneType object and shows output on the cmd
but i want output in string format, i tried using exec() but it didn't work for me.There is some way to do that?
import Tkinter as tk
from Tkinter import *
from ScrolledText import *
from code import InteractiveInterpreter
interpreter = InteractiveInterpreter()
def go(event):
print(interpreter.runcode(textPad.get('1.0', END+'-1c')))
root = tk.Tk()
textPad = ScrolledText(root,width=100,height=100)
textPad.focus_set()
b2=tk.Button(root, text ="Run",width=34,height=3)
b2.pack()
b2.bind('<Button-1>', go)
textPad.pack()
root.mainloop()

Related

tkinter askdirectory doesn't work from imported module

I have been using tkinter to supply a file dialog (in python 3.6) which allows users to select a directory. It works fine when it is a sub-function within the same module, but if I move that subfunction into a separate module and then try to import it from that module, it no longer works. Instead the code just hangs when the file dialog should pop up but it never appears.
working code:
This works if I run the main function
from tkinter import Tk
from tkinter.filedialog import askdirectory
def SelectDirectory():
# start up the tk stuff to have a file directory popup
print('start')
root = Tk()
print('postroot')
root.withdraw()
print('postwithdraw')
# let the user pick a folder
basepath = askdirectory(title='Please select a folder')
print('postselection')
root.destroy()
print('postdestroy')
return basepath
def main():
ans = SelectDirectory()
print(ans)
If I instead put this in another module and import it from that module, then it will print until 'postwithdraw' and then just hang.
submod.py:
from tkinter import Tk
from tkinter.filedialog import askdirectory
def SelectDirectory():
# start up the tk stuff to have a file directory popup
print('start')
root = Tk()
print('postroot')
root.withdraw()
print('postwithdraw')
# let the user pick a folder
basepath = askdirectory(title='Please select a folder')
print('postselection')
root.destroy()
print('postdestroy')
return basepath
and then run this:
from submod import SelectDirectory
def main():
ans = SelectDirectory()
print(ans)
It never gets past 'postwithdraw' and the file dialog never pops up.
Does anyone know what I am doing wrong here? I would think it has something to do with the tkinter window not appearing since its not the main module but is there some way to get past that?
Your both versions don't work. Both give 'module' object is not callable.
You have to use
root = Tk.Tk()
instead of
root = Tk()
and then both versions works.
Maybe two Tk in Tk.Tk() seems weird but usually we use lowercase tk instead of Tk in
import tkinter as tk
and then you have
root = tk.Tk()

new window title in tkinter

I'm new to tkinter in python and am not able to figure out what the syntax below does exactly.
oldtitle=window.newtitle()
Removing this line from the code doesn't makes any difference to the output.
from tkinter import *
from tkinter import ttk
root=Tk()
root.title('to')
main=Toplevel(root)
tk=main.title()#<---this line
main.title('hello world')
mainloop()
What the line tk=main.title() does is to get the title of main.
Here is a demo using your own code:
from tkinter import *
from tkinter import ttk
root=Tk()
root.title('to')
main=Toplevel(root)
main.title('hello world')
tk=main.title() # Note I moved this line to here
print(tk) # This will print 'hello world'
mainloop()
The line print(tk) will print the title of main which is hello world.
If you want to set a different title then use this synatax instead: tk = main.title('Some new title') (or simply main.title('Some new title') if you do not need to save the title string into an other variable):
from tkinter import *
from tkinter import ttk
root=Tk()
root.title('to')
main=Toplevel(root)
main.title('hello world')
tk=main.title('Some new title') # or simply: main.title('Some new title')
mainloop()
Output:
Note: avoid using tk as your personal variable name because the recommended way to import tkinter is: import tkinter as tk

Output result in a new Window

Basically, I have a root window that has a couple of buttons that runs different Python code, and it outputs the result into the console.
Is there a way to display the output into a new window (pop-up window) once the user clicks the button?
You could make a message box by doing
The Imports:
import Tkinter
from Tkinter import *
import tkMessageBox
import ttk
Heres the Code:
window = Tkinter.Tk()
window.wm_withdraw()
#centre screen message
window.geometry("1x1+"+str(window.winfo_screenwidth()/2)
+"+"+str(window.winfo_screenheight()/2))
tkMessageBox.showinfo(title=" ", message=" ")

Why always have to force quit python launcher after running Tkinter on Mac?

I'm using jupyter notebook on mac, recently I need to write interactive dialog box, so after google, I use Tkinter to make an interactive window.
But I was bothered by this problem couples day ,and still can't find a solution way.
Fisrt example:
from Tkinter import *
from tkFileDialog import askopenfilename
import sys
import os,time
def callback():
name= askopenfilename()
print name
errmsg = 'Error!'
Button(text='File Open', command=callback).pack(fill=X)
mainloop()
Second example:
from Tkinter import *
import sys,os
class YourApp(Tk):
def quit_and_close(self):
app.quit()
#os._exit(0)
#sys.exit(1)
#exit(0)
app = YourApp()
app.title('example')
app.geometry('400x300+200+200')
b = Button(app, text = "quit", command = app.quit_and_close)
b.pack()
app.mainloop()
And the third one:
import Tkinter as tk
import tkMessageBox
def ask_quit():
if tkMessageBox.askokcancel("Quit", "You want to quit now? *sniff*"):
root.destroy()
root = tk.Tk()
root.protocol("WM_DELETE_WINDOW", ask_quit)
root.mainloop()
After running those above code, always need have to force quit python launcher.
It is very weird, and annoying because after forcing quit, I will got the error:
Is it necessary to use python launcher as default window?
Is there possible to set another window to open ?
or is there proper way to close the launcher without causing programming crash?
p.s Even I try to use wxpython, it still open python launcher and got the same problem.

problems with the update of a label Tkinter

I'm really stuck on a basilary things: I have this code
from tkinter import *
import sys
import subprocess
import tkinter as tk
def cd():
f=(subprocess.check_output("net view"))
e=(f.decode(sys.stdout.encoding))
label1=Label(text=e).pack()
def mainscreen():
mainscreen=Tk()
mainscreen.title("Terfysgol's kit V 2.0")
frame1=Frame(mainscreen)
frame1.pack()
puls1=Button(frame1,text="List of device", borderwidth= "2",command= cd).pack()
mainscreen()
When I run it all the time that I press the button it create a new label but I only want to update the text of the label1.
This is what you are after:
def cd():
f=(subprocess.check_output("net view"))
e=(f.decode(sys.stdout.encoding))
label1.config(text = e)
and then at the top of your program after your imports you need to put:
label1 = Label()
label1.pack()
Please note that I'm not suggesting this is good program structure, but that is up to you to sort out. This answer is just a quick fix to provide you with enough information to work out the rest of what you need.
Also you can remove the import tkinter as tk line already imports tkinter.

Categories

Resources