When i press the start button, it runs the other script but only shows it in the other terminal, not in the GUI scrolledtext box i have. is there a simple fix for my problem? not finding an answer anywhere.
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import sys
import os
import tkinter.scrolledtext as tkst
root = Tk()
root.title("WLP")
root.geometry("950x450")
root.iconbitmap("Vo1d.ico")
root.configure(bg='#0c0c0c')
def clickstart():
os.system('python WLP.py')
Button1 = Button(root, text="START", bg="#0c0c0c", fg="#C0C0C0", command=clickstart) #)
Textbox = tkst.ScrolledText(root, width=75, height=10, bg="#0c0c0c", fg="#C0C0C0")
Button1.grid(row=10, column=5, pady=15)
Textbox.grid(row=17, column=5)
root.mainloop()
This is far from a good way to do it, but this should work (after importing subprocess)
def clickstart():
output = subprocess.run(['python', 'WPL.py'], stdout=subprocess.PIPE, check=True)
output = str(output.stdout, encoding='utf-8')
Textbox.insert(1.0, output)
Related
I am eriting code in pycharm with tkinter but the window is not opening. May someone assist?
`
import tkinter
window = tkinter.Tk()
button = tkinter.Button(window, text="Do not press this button! >:-(", width=40)
button.pack(padx=10, pady=10)
`
i tried checking my script for bugs but nothing
This has nothing to do with Pycharm, but with tkinter library and how to use it.
You are missing 2 important stuff:
Button is in ttk.py file inside tkinter library: from tkinter import ttk
Execute the whole script with mainloop
Try this:
import tkinter
from tkinter import ttk # Import ttk file from tkinter library
window = tkinter.Tk()
window.title("Coolest title ever written")
button = ttk.Button(window, text="Do not press this button! >:-(", width=40) # Use Button from the import ttk file
button.pack(padx=10, pady=10)
window.mainloop() # Execute the whole script
I ran into a problem where I want to click a button on a fullscreen app.
test1
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os
root = Tk()
root.title('Gamesim')
root.geometry('500x400')
def cmdopen():
os.system('C:\Users\User\Desktop\test2.py')
btn = Button(text='test', command=cmdopen)
btn.pack()
root.mainloop()
test2
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os
root = Tk()
root.title('Gamesim')
root.geometry('1870x1080')
root.attributes("-topmost", True)
btn = Button(text='test2')
btn.pack()
root.mainloop()
What it does it displays the test2 interface, but test one stops responding. What I want is that the test2 will apear above and both will respond and are diffrent windows.
Im bad in english so sorry if I have some problems.
If you're okay with having one "master" window that keeps track of the other windows, then you can do something like this:
from tkinter import *
from tkinter.ttk import *
from functools import partial
class subWindow(Toplevel):
def __init__(self, master=None):
super().__init__(master=master)
def createSubwindow(master):
"""Creates a subWindow of 'master' and sets it's options"""
subWin = subWindow(master)
subWin.title('SubWindow')
subWin.geometry('500x400')
subWin.attributes("-topmost", True)
btn = Button(subWin, text='Button Inside of SubWindow')
btn.pack()
# Creating the master-window
root = Tk()
root.title('MasterWindow')
root.geometry('500x400')
# Creates a partial of the createSubwindow, so that we can execute it easier in the button.
subWinPartial = partial(createSubwindow, root)
# Installs the button, with the partial function as a command.
btn = Button(root, text='Create Sub Window', command=subWinPartial)
btn.pack()
# Runs the mainloop, that handles all the windows.
root.mainloop()
so i want to open a new python tkinter program when i click a button in the GUI. As soon as i run the code ProgramB runs. When i close this GUI the main GUI runs but when i click the button in this GUI, it doesn't open ProgramB. I'm Learning pyhton and tkinter and don't know how to fix this problem. Thank you in advance.
My main code:
from tkinter import *
from tkinter.ttk import *
import basis_tkinter
import subprocess
mainwindow = Tk()
mainwindow.title= "test opening new file"
def open():
subprocess.call("basis_tkinter.py", shell=True)
btnopen = Button(mainwindow, text="open new program", command=lambda: open).pack()
mainwindow.mainloop()
Program B (basis_tkinter):
from tkinter import *
MainWindow = Tk()
MainWindow.title("Title")
MainWindow.iconbitmap(r'C:\Users\Gebruiker\Documents\school 2020-2021\stage\interface\Afbeeldingen\Apex icon.ico')
Background= Label(MainWindow, image=PhotoImage(file=r"C:\Users\Gebruiker\Documents\school 2020-2021\stage\interface\Afbeeldingen\Apex logo.png"))
Background.place(x=0,y=0, relwidth=1, relheight=1)
MainWindow.geometry("400x400")
def clicked():
label = Label(MainWindow, text="You clicked").pack()
button = Button(MainWindow, text="Click me", command=clicked).pack()
MainWindow.mainloop()
Why does tkinter.Button() appear as some ancient OS style button, while a messagebox like tkinter.messagebox.showinfo() comes with an OK button using the current version of the OS?
My OS is Windows. Not sure if this problem exists on Mac OS, but either way the people using my tools are on Windows.
An example snippet of code I found here shows how the buttons are different.
Image:
Question:
Is there a way to make tkinter.Button() look like the button inside a messagebox, which seems to be using the current OS style?
Code:
from tkinter import *
from tkinter import messagebox
window = Tk()
window.title("Welcome to LikeGeeks app")
window.geometry('350x200')
def clicked():
messagebox.showinfo('Message title', 'Message content')
btn = Button(window,text='Click here', command=clicked)
btn.grid(column=0,row=0)
window.mainloop()
Use tkinter.ttk to get themed version
from tkinter.ttk import *
from tkinter import messagebox
window = Tk()
window.title("Welcome to LikeGeeks app")
window.geometry('350x200')
def clicked():
messagebox.showinfo('Message title', 'Message content')
btn = Button(window,text='Click here', command=clicked)
btn.grid(column=0,row=0)
window.mainloop()
doc
You can use tkinter.ttk which provides a modern OS style theme to your TK widgets. Example from the Python tkinter docs:
from tkinter import ttk
import tkinter
root = tkinter.Tk()
ttk.Style().configure("TButton", padding=6, relief="flat",
background="#ccc")
btn = ttk.Button(text="Sample")
btn.pack()
root.mainloop()
#Output:
I'm making a stock control system for my Computer Science A level coursework. The problem I have is I do not know how to get python to launch a different python program after Button1 is clicked.
from tkinter import *
top=Tk()
Button1= Button(top,text='UPDATE STOCK', width=40)
Button1.place(x=80, y=20)
mainloop()
import os
from tkinter import*
import subprocess
def otherlaunch():
subprocess.call(['python.exe', "filename.py"]) # filename.py is the file
top=Tk()
top.title('Stock Control')
top.geometry('400x200')
Button1= Button(top,text='UPDATE STOCK', width=40,command=otherlaunch)
Button1.place(x=80, y=20)
mainloop()
from Tkinter import Tk, Button, mainloop
import subprocess
def ext_python_script(event):
subprocess.call(["python2.7", "sss.py"])
if __name__ == '__main__':
top = Tk()
Button1 = Button(top, text='UPDATE STOCK', width=20, height=10)
Button1.place(x=10, y=20)
Button1.bind("<Button-1>", ext_python_script)
mainloop()
We can use bind here.
You can use the import statement to load one python program from another, and the command parameter to do something on button click, like this:
import os
from tkinter import*
top=Tk()
top.title('Stock Control')
top.geometry('400x200')
def run_other():
import filename #notice that it is not a string, and there is no '.py'
Button1= Button(top,text='UPDATE STOCK', width=40, command=run_other)
Button1.place(x=80, y=20)
mainloop()