tkinter basic queries with python 3.7 - python

I am new to tkinter
import datetime as dt
from tkinter import *
from tkinter import messagebox
dt1 = dt.datetime.now().strftime("%Y""%m""%d")
time1 = dt.datetime.now().strftime('%H:%M:%S')
msg1= dt1+" "+time1+ " Test Message XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
master = Tk()
top = Tk()
top.geometry("100x100")
messagebox.showinfo("information", msg1)
top.mainloop()
I have basically 3 issues in this basic code
It is creating additional blank windows along with my message which is not required. How to avoid this ?
I want to display my message into single line how can i do this ?
How to change font and color of this message ?

Related

Python guı text insert function result

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 .

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

Error with TKinter Attribute Simpledialog

I'm currently trying to write in a few basic user input boxes using the tkinter module in Python 3.6 (via Spyder). I can confirm that the module loads, and that the option to select simpledialog comes up, but I keep getting the following error:
AttributeError: module 'tkinter' has no attribute 'simpledialog'
Image of tkinter simpledialog
I've tried to look for other options, but other user input options do not seem to work on my Python interface. It either crashes, or the data isn't structured properly.
Interestingly enough, in the past, I've done similar things in Python with no errors, but this keeps coming up with this particular programming piece.
import tkinter as tk
import pyodbc as py
py.pooling = False
## INPUT YOUR USER ID AND PASSWORD AND DECLARE YOUR CONNECTION
## THE DIALOG BOXES MAY POP OPEN ON ANOTHER SCREEN
## THE PASSWORD INPUT IS MASKED AND WILL NOT SHOW IN THE
## VARIABLE EXPLORER
ID = tk.simpledialog.askstring("Please input your username.","Username: ")
PW = tk.simpledialog.askstring("Please input your password.",
"Password: ", show='*')
CONN = tk.simpledialog.askstring("Please input your connection.",
"Connection: ")
My expected results are that a popup window will appear and that I'll be able to get the user information I need to maintain a stable connection to the server I'm using.
Thank you in advance for your advice!
simpledialog is not in tkinter but in tkinter.simpledialog and you have to import it
import tkinter as tk
import tkinter.simpledialog
root = tk.Tk() # create main window
#root.iconify() # minimize main window
root.withdraw() # hide main window
answer = tkinter.simpledialog.askstring("Question", 'Your name:')
print(answer)
#root.destroy() # should work without it
#root.mainloop() # should work without it
See tkinter modules
import tkinter as tk
from tkinter import simpledialog
root = tk.Tk()
ID = simpledialog.askstring("Please input your username.", "Username: ", parent=root)
root.mainloop()
This will keep the popup within the parent window and visible.

Python running clock in separate window

I thought I would make a running clock program. I have this code which works for what I want it to do, but I want it to be fancy and output it to a new window. I thought of a message box but that would need constant closing.
Is there way around this, or should I just stick to using the console?
x=0
import datetime
import time
while x<10:
currentTime=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
time.sleep(1)
print(str(currentTime))
EDIT:
This is what i have now but the window goes all over the place.
try:
from Tkinter import *
except ImportError:
from tkinter import *
import datetime
import time
x=0
while x<10:
root = Tk()
prompt = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
label1 = Label(root, text=prompt, width=len(prompt))
label1.pack()
def close_after_1s():
root.destroy()
root.after(1000, close_after_1s)
root.mainloop()
You're missing the point of Tk. The entire thing is a loop(hence the mainloop) and you keep destroying and creating a new window, hence the all over the place.
I think you just want something to update every sec:
from Tkinter import Tk,StringVar,Label
import datetime
def update():
global prompt,root
prompt.set(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
root.after(1000, update)
root = Tk()
prompt = StringVar()
label1 = Label(root, textvar=prompt, width=len(prompt.get()))
label1.pack()
update()
root.mainloop()
and my suggestion is to put all of this in a class. Google some Tk examples.

Changing source file while program is running

I am trying to change a program's behaviour on the fly by editing its source file (under Python 3.4.2, Windows 8.1). My program is a tkinter GUI with a button that sums two values, but I want to be able to change the button's behaviour. I am currently trying to do this by editing the source file (changing, say, the addition to subtraction), saving it, and then clicking a button whose callback function imports the source file. I want my code changes to be reflected in the running GUI without having to exit and restart the program. I also want this import to only recompile the lines that I changed, rather than the entire file.
The program, reload0.py:
import time
import serial
import sys
import os
import tkinter as tk
from tkinter import ttk
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
try:
import Tkinter # Python 2
import ttk
except ImportError:
import tkinter as Tkinter # Python 3
import tkinter.ttk as ttk
mGui = Tk()
mGui.title("GUI")
mGui.geometry('400x200+100+100')
def mOp():
num1 = value1.get()
num2 = value2.get()
Op=num1+num2
name1.set('Sum')
name2.set(Op)
def mReLoad():
import reload0.py
mGui.update()
def mCheck():
if len(name1.get()) == 0:
name1.set('name1')
mGui.update()
if (len(name2.get()) == 0):
name2.set('name2')
mGui.update()
try:
print(value1.get())
except ValueError:
value1.set(0)
mGui.update()
try:
print(value2.get())
except ValueError as ValE:
value2.set(0)
mGui.update()
print(ValE)
value1 = DoubleVar()
value2 = DoubleVar()
name1 = StringVar()
name2 = StringVar()
mButtonSave = Button(mGui, text = "Operation", command = mOp, fg = 'Red').place(x=150,y=80)
mButtonLoad = Button(mGui, text = "ReLoad Operation", command = mReLoad, fg = 'Red').place(x=150,y=110)
mButtonLoad = Button(mGui, text = "Check", command = mCheck, fg = 'Red').place(x=150,y=140)
tText1 = Entry(mGui, textvariable = name1).place(x=10,y=80)
tText2 = Entry(mGui, textvariable = name2).place(x=10,y=100)
vText1 = Entry(mGui, textvariable = value1).place(x=10,y=120)
vText2 = Entry(mGui, textvariable = value2).place(x=10,y=140)
For your purpose of changing button functionality there are easy ways than changing source code, and as commented it's not possible.
judging from another question i saw of yours you are quite new to python programming. I would recommend spending some time on some basic tutorials getting to know python and some programming concepts first.
for instance
import reload.py
should simply be
import reload
https://wiki.python.org/moin/BeginnersGuide/NonProgrammers
read a book, do some examples, and so enough you will be the one answering the questions here, good luck!

Categories