Python guı text insert function result - python

Hello everyone how can insert in gui text function result of another module. you can see code below:
modul_1.py
import tkinter as tk
from tkinter import *
import modul_2
window = tk.Tk()
b = Button(text="çalıştır", command = modul_2.goster)
b.place(x=20, y=20)
window.mainloop()
modul_2.py
import tkinter as tk
from tkinter import messagebox
import modul_1
def goster():
messagebox.showinfo("deneme","deneme")
Text1 = tk.Text(window)
Text1.insert(tk.END, "Bu kısım")
Text1.place(x=50, y=50)
goster()

The better way is to stock the data from the module you want then import it in the desired module .

Related

Tkinter ScrolledText widget not appearing

I'm trying to add the ScrolledText widget to a Tkinter window. The program reads it perfectly as in it accepts the INSERT method for it with no errors but it's not showing up. The problem came up when I added Notebook Tabs. I've attached the code snippet. I used the place() method because I need the rest of my buttons and labels arranged in a specific pattern.
import tkinter
from tkinter import *
from tkinter import scrolledtext
from tkinter import messagebox
from tkinter import ttk
import os
import datetime
# Variables
window = Tk()
window.title("Vesnica Pomenire")
window.geometry('1500x1000')
var = IntVar()
var.set(1)
txt = scrolledtext.ScrolledText(window,width=40,height=10)
txt.place(x=50, y=50)
You're missing the mainloop()
import tkinter
from tkinter import *
from tkinter import scrolledtext
from tkinter import messagebox
from tkinter import ttk
import os
import datetime
# Variables
window = Tk()
window.title("Vesnica Pomenire")
window.geometry('1500x1000')
var = IntVar()
var.set(1)
txt = scrolledtext.ScrolledText(window,width=40,height=10)
txt.place(x=50, y=50)
window.mainloop() #You are missing this
You can read more about mainloop() here
You really missed mainloop command
window.mainloop()
add this at the bottom of your code and it will do the thing

Tkinter Gui Not running when .py file is opened

I'm working on this project and I deiced to add a GUI interface. I have chosen to work with Tkinter cause I'm some sort familiar with Python. I'm running into the problem where I can run the GUI out of visual studio but I am unable to run the GUI off straight of my desktop. I have checked and there are no errors in my code. Can someone please help me fix the code.
This Is a snippet of the code which I am using to run the GUI
from tkinter import *
from tkinter.ttk import Progressbar
from tkinter import ttk
from tkinter import messagebox
import os
import shutil
from os import listdir
from os.path import isfile, join
import getpass
import time
window = Tk()
window.title("Move Files")
window.geometry('546x500')
def Movie():
TextBox.delete('1.0',END)
bar['value'] = 0
messagebox.showinfo('Message title', 'Message content')
def TVShow():
TextBox.delete('1.0',END)
bar['value'] = 0
TVShowMove()
#Buttons
Movie = Button(window,text='Move Movies', command=Movie, padx=50, pady=30)
Movie.place(x=40, y=40)
TVShow = Button(window,text='Move TV Shows', command=TVShow, padx=48, pady=30)
TVShow.place(x=300, y=40)
#Progressbar
bar = Progressbar(window, length=446, style='black.Horizontal.TProgressbar')
bar.place(x=40, y=140)
#TextBox
TextBox = Text(window, height=10, width=55)
TextBox.pack()
TextBox.place(x=40, y=170)
window.mainloop()
code is ok
i got this problem too!
try to open it from vscode itself
In Python 3.8 you cant import any of these.
from tkinter.ttk import Progressbar
from tkinter import ttk
from tkinter import messagebox
Instead, you have to import Tkinter as tk as seen below
from tkinter import *
import tkinter as tk

I keep getting an error when I try to import tkFileDialog

I am following this tutorial to create my own simple text editor. However, I am coming across an error I don't know how to fix. I'm running this code:
from tkinter import *
import tkinter.tkFileDialog
I've also tried this:
import tkinter
import tkinter.tkFileDialog
Both of them give me this error:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import tkinter.tkFileDialog
ModuleNotFoundError: No module named 'tkinter.tkFileDialog'
I'm doing exactly what the tutorial said to do, but it won't work. Why is this happening?
For reference, I am using python 3.7 64-bit on windows 10.
Python 3 tkinter does not have a tkFileDialog import. Instead you want to import filedialog like this.
import tkinter as tk # this is the preferred import for tkinter
from tkinter import filedialog
root = tk.Tk()
x = filedialog.askopenfilename()
print(x)
root.mainloop()
If you would prefer to only import the dialog's you specifically need you can do something like this.
import tkinter as tk # this is the preferred import for tkinter
from tkinter.filedialog import askopenfilename
root = tk.Tk()
x = askopenfilename()
print(x)
root.mainloop()
Update: Based on what Bryan has mentioned in the below here is another example that includes a delay to help prevent the issue mentioned. Though this only applies to dilogs opened before the mainloop has been reached and for many applications I would think this is not an issue as dialog is not often the first thing you have up in a GUI. However it is still good information to have.
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
def print_file_name():
x = filedialog.askopenfilename()
print(x)
root.after(100, print_file_name)
root.mainloop()
Or:
import tkinter as tk # this is the preferred import for tkinter
from tkinter.filedialog import askopenfilename
root = tk.Tk()
def print_file_name():
x = askopenfilename()
print(x)
root.after(100, print_file_name)
root.mainloop()

tkinter entry widget that takes in images

I want to make a Tkinter interface for a program I just wrote. The interface needs to have a widget where the user can insert an image for the program to process. I couldn't find such a widget online. How would I make such an interface?
Below is an example of tkinter entry widget that takes in images right away:
try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
import tkinter.filedialog as tkfd
except ImportError:
import Tkinter as tk
import tkFileDialog as tkfd
if __name__ == '__main__':
root = tk.Tk()
entry = tk.Entry(root)
entry.pack()
path = tkfd.askopenfilename(initialdir = "/", title = "Select file",
filetypes = (("jpeg files","*.jpg"),("all files","*.*")))
entry.insert('0', path)
root.mainloop()

remove {} from Tkinter label

import Tkinter
window=Tk()
msg1='abc'
msg2='def'
label =Label(window,text=(msg1,msg2), fg='blue')
label.grid(row=0,column=1)
window.mainloop()
output is {abc}{def} I want output as abcdef. how to do that?
You're writing text=(msg1,msg2) with (msg1,msg2) being a list.
Just change it to msg1+msg2
And this code cannot work, you can't create a Tk() if you import Tkinter like that.
Change it to from Tkinter import *

Categories