I am using Tkinter in ArcMap Add-in python code. Tkinter UI is opening well but then it closes or crashes ArcMap.
I am using the below code.
import arcpy
import pythonaddins
from Tkinter import *
import tkMessageBox
class ButtonClass1(object):
"""Implementation for Testing_addin.button (Button)"""
def __init__(self):
self.enabled = True
self.checked = False
def onClick(self):
root = Tk()
lbl = Label(root, text = "Hello")
lbl.pack()
btn = Button(root, text = "OK")
root.mainloop()
pass
Thank you!
Related
I am working on a project that uses buttons, but I would like to make it modular. The only problem I can't seem to get pass by is getting if someone is pressing the button from a different file
-file 1
from tkinter import*
class app2:
def __init__(self):
s = Tk()
but = Button(text="Test",command=subcommand)
but.pack()
s.mainloop()
def subcommand():
x = command()
x.command
class command:
def __init__(self,int):
self.command = ()
y = app2()
file 2-
from tkinter import*
from idea2 import *
def clicked():
print("HI")
x = command()
x.command(clicked())
My code basically just takes a function from another file and loads it into the command class. So basically the button can get different commands from different files. I don't know what to put for "x = command(???)" because I am trying to get that from a different file.
Basic question:
How to make my Tkinter button modular.
just pass the callback into the app2 constructor
file1
from tkinter import*
class app2:
def __init__(self,btn_callback=subcommand):
s = Tk()
but = Button(text="Test",command=btn_callback)
but.pack()
s.mainloop()
file2
from idea import app2
def my_callback(*args,**kwargs):
print("Buttton pressed...")
app = app2(my_callback)
or better yet make use of the event system in tkinter ... something like this
import tkinter as Tkinter
import tkinter.messagebox as tkMessageBox
def helloCallBack(*a,**kw):
tkMessageBox.showinfo( "Hello Python", "Hello World")
class MyApp(Tkinter.Tk):
def __init__(self):
super().__init__()
self.b = Tkinter.Button(self, text ="Hello", command = lambda:self.event_generate("<<button_click>>"))
self.b.pack()
app = MyApp()
app.bind("<<button_click>>",helloCallBack)
app.mainloop()
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()
from tkinter import*
import tkinter.messagebox
from tkinter import ttk
import random
import time
import datetime
def main():
root = Tk()
app = Login(root)
class Login:
def __init__(self, master):
self.master = master
self.master.title("Billing Login System")
self.master.geometry("1350x750+0+0")
self.master.config(bg = 'cadet blue')
self.frame = Frame(self.master,bg='cadet blue')
self.frame.pack()
#Some code here
..(Login Conditions)
..
#
#After authentication this window should pop up
class customer:
def __init__(self, root):
self.root = root
self.root.title("eZ Billing System")
self.root.geometry("1350x750+0+0")
self.root.config(bg="cadet blue")
self.frame = Frame(self.root,bg='cadet blue')
self.frame.pack()
#some code here
if __name__ == '__main__':
main()
This code works but the problem is that when i run the file no error shows up or warnings and neither does any window shows but if i run any other python program then this windows pops up and no problems.
I am new to this and cant figure out whats wrong.
I guess you aren't creating main window instance.
That's why it is not showing error but not showing any output even.
Try adding this :
root.mainloop()
So I have a program that takes the most recent outlook e-mail and displays it once a button is pressed. What I want to do is get rid of the button and have the answer_label automatically run the timer function to display the e-mail at all times. Any suggestions?
import win32com.client
import os
import threading # use the Timer
import tkinter
from tkinter import Tk, Label, Button
class myGUI:
def timer(self):
import pythoncom # These 2 lines are here because COM library
pythoncom.CoInitialize() # is not initialized in the new thread
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case,
# the inbox. You can change that number to reference
# any other folder
messages = inbox.Items
message = messages.GetLast()
body_content = message.Body
self.answer_label['text'] = (body_content) # orginally used body_content.encode("utf-8") to fixed character encoding issue
# caused by Windows CMD. But then figured out you can type 'chcp 65001' in cmd
threading.Timer(5, self.timer).start() # refresh rate goes here
def __init__(self, master):
self.master = master
master.title("CheckStat")
self.answer_label = Label(master, text='')
self.answer_label.place(height=300, width=300)
self.greet_button = Button(master, text="Start", command=self.timer)
self.greet_button.place(height=20, width=100)
def greet(self):
print("Greetings!")
root = Tk()
my_gui = myGUI(root)
root.mainloop()
You do not need a button explicitly to run your timer function. Just call it within init. This piece of code works for me (it displays time instead of email).
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import datetime
import threading # use the Timer
from tkinter import Tk, Label, Button
class myGUI:
def timer(self):
self.answer_label['text'] = datetime.datetime.now()
threading.Timer(5, self.timer).start()
def __init__(self, master):
self.master = master
master.title("CheckStat")
self.answer_label = Label(master, text='')
self.answer_label.place(height=300, width=300)
self.timer()
root = Tk()
my_gui = myGUI(root)
root.mainloop()
Be careful with using tkinter in multi-threaded applications.
I'm trying to open a .txt file with Python. I'm trying to fill a Tkinter text widget with the files contents.
However with the following snippet, when I try to open the files contents and put it in a the text widget self.Te, nothing happens. Any clues?
Snippet:
self.Open = tkFileDialog.askopenfilename(initialdir='C:')
text_file = open(self.Open, "r")
# self.Te is a text widget
self.Te.insert('1.0', text_file.read())
Here is a working example:
#!/usr/bin/env python
from Tkinter import *
from tkFileDialog import askopenfilename
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.button = Button(frame, text="QUIT", command=frame.quit)
self.button.pack(side=BOTTOM)
self.text = Text(frame)
self.text.pack(side=TOP)
self.choosen = askopenfilename(initialdir='~')
self.text.insert(END, open(self.choosen).read())
root = Tk()
app = App(root)
root.mainloop()
See also Text widget method documentation:
http://www.pythonware.com/library/tkinter/introduction/x8369-methods.htm
... Insert text at the given position (typically INSERT or END) ...